我有一个问题要找到非常有效的方法来搜索许多列表中的生日。
这是我的horoscope_dates函数
def calculate_horoscope_dates(start, end):
horoscope_start = datetime.datetime.strptime(start, "%m-%d-%Y")
horoscope_end = datetime.datetime.strptime(end, "%m-%d-%Y")
horoscope_interval = [horoscope_start + datetime.timedelta(days=x) for x in range(0, (horoscope_end-horoscope_start).days)]
string_interval = []
for date in horoscope_interval:
#convert datetime to str
string_interval.append(date.strftime("%d-%m-%Y"))
# remove minutes and seconds from date
string_interval = [i.lstrip("0") for i in string_interval]
return string_interval
这是我的星座表,如下所示
aries = calculate_horoscope_dates("3-21-2020", "4-20-2020")
taurus = calculate_horoscope_dates("4-20-2020", "5-21-2020")
gemini = calculate_horoscope_dates("5-21-2020", "6-22-2020")
...
aries列表定义如下所示。其他列表与定义的列表相同。
['21-03-2020',
'22-03-2020',
'23-03-2020',....]
这是我的输入部分
month = input("Enter your month of birth: " )
day = input("Enter your day of birth: " )
birthday = day + "-0" + month + "-2020"
birthday
我可以通过for循环执行搜索过程,但是我认为这不是一种非常有效的方法,因为我可以重复编写下面的代码。
for birthday_date in aries:
if birthday in birthday_date:
print("Aries")
我想问的是学习如何有效地在许多清单中找到生日。