2021年2月6日星期六

Python dictionary update function very slow

My function to update a dictionary is prohibitively slow; can anyone help me spot how I could improve it? FYI, the original dataset is a list of dictionaries. Each dictionary reports data on the number of bicycles counted at a park at one hour of the day. My goal is to end up with a dictionary with date: (daily count) as key:value.

The function that is taking forever:

def dict_of_counts(datelist):      master_dict = {}      for date in datelist:          master_dict.update({date: (dailysum(date))})      return master_dict    

For reference, dailysum is a function that sums up the 24 hours of counts to get the total counts for one day:

def dailysum(date):      strlist = hourlycounts(date)  #hourlycounts() produces a list of counts for each hour of a given day.      try:           intlist = list(map(int, strlist)) #converts list of strings to integers          return sum(intlist)       except ValueError:           return 0  

I have been running dict_of_counts with a list of dates for datelist, as follows:

[datetime.date(2020, 4, 19),   datetime.date(2020, 4, 20),   datetime.date(2020, 4, 21),   datetime.date(2020, 4, 22),   datetime.date(2020, 4, 23)]  

I have tried making datelist different lengths. It works ok with just five dates, as shown above, but when I try even 30 dates it takes several minutes, and my attempts at running it with a whole year's worth of dates (my ultimate goal) run for over 30 minutes before I give up.

I've searched around but am feeling very stuck. Thank you!

https://stackoverflow.com/questions/66083895/python-dictionary-update-function-very-slow February 07, 2021 at 10:03AM

1 条评论: