Reputation: 2701
I have a problem to find very efficient way to search birthday in many list.
Here is my horoscope_dates function
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
Here is my horoscope list shown below
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 list is defined shown below. Other list is the same as a defined list.
['21-03-2020',
'22-03-2020',
'23-03-2020',....]
Here is my input part
month = input("Enter your month of birth: " )
day = input("Enter your day of birth: " )
birthday = day + "-0" + month + "-2020"
birthday
I can do search process via for loop but I think it is not very efficient way to do the process because I can write repeatly this code show below.
for birthday_date in aries:
if birthday in birthday_date:
print("Aries")
What I want to ask is learn how to find birthday in many list very efficiently.
Edit
horoscope_dates = {"aries": aries,
"taurus": taurus,
"gemini": gemini,
"cancer": cancer,
"leo": leo,
"virgo": virgo,
"libra": libra,
"scorpio": scorpio,
"sagittarius": sagittarius,
"capricorn": capricorn,
"aquarius": aquarius,
"pisces": pisces,
}
for h in horoscope_dates:
start, end = horoscope_dates[h] <- Error
if start <= birthday < end:
print(h)
There is an issue there (ValueError: too many values to unpack (expected 2))
Upvotes: 0
Views: 53
Reputation: 6056
from datetime import date
horoscope_dates = {"aries": (date(2020, 3, 21), date(2020, 4, 20)),
"taurus": (date(2020, 4, 20), date(2020, 5, 21)),
"gemini": (date(2020, 5, 21), date(2020, 6, 22)),
}
month = int(input("Enter your month of birth: "))
day = int(input("Enter your day of birth: "))
year = 2020
birthday = date(year, month, day)
for horoscope, (start, end) in horoscope_dates.items():
if start <= birthday < end:
print(horoscope)
break
Upvotes: 1