如何将月相度数映射到Python列表中的项目

问题描述 投票:0回答:1

我正在使用 Skyfield API 来计算特定时间的相位。相位本身是没有意义的,我想命名度数所代表的相位。 0° 表示新月。 45°表示蜡月形。

moon_phases=['New Moon','Waxing Crescent', 'First Quarter','Waxing Gibbons','Full Moon','Waning Gibbons', 'Last Quarter', 'Waning Gibbons']
degrees=[0,45,90,135,180,225,270,315]

即使相位度数为 340° 或 20°,我也希望它打印新月。对此最好的方法是什么?我不想使用 if else。

python coordinate-transformation astronomy
1个回答
0
投票

解决方案

一个简单的解决方案是确定您的学位中有多少次是 45。

import math

moon_phases=['New Moon','Waxing Crescent', 'First Quarter','Waxing Gibbons','Full Moon','Waning Gibbons', 'Last Quarter', 'Waning Gibbons']
degrees=[0,45,90,135,180,225,270,315]

degree = 20
i = math.trunc(degree / 45)
moon_phase = moon_phases[i]

print(moon_phase)
>> New Moon
© www.soinside.com 2019 - 2024. All rights reserved.