Python日期 - 编程困境



我正在使用 datetime

我有一个用户时间表列表,这些时间表是在本周(周日至周六)中选择的

timesheet_entries = [entry for entry in timesheet_entries if within_dates(datetime.datetime.strptime(entry["work_performed_on"], "%Y-%m-%d").date())]

我需要检索每天每天提交的时间(每天可能有一个以上的条目)。entry["work"]返回成员为该条目花费的时间。

有什么想法吗?我并不是在这里寻找代码的答案,而是更多的逻辑。例如,我可以要求每天的参赛作品,例如monday = [entry for entry in timesheet_entries if within_dates (monday etc)],然后循环循环,每天都在努力并做同样的时间。

目前尚不清楚如何通过结果。假设您想要一周中每天工作的总小时数(时间)的总数,请尝试此方法。

由于固定了一周的日子,只需循环循环。对于每天,过滤当天的条目,然后收集时间。您可以使用defaultdict,关键是工作日,每个条目都是当天所有时间的列表。

from collections import defaultdict
total_times = defaultdict(list)
for i in range(0,7):
   total_times[i] = [entry['work'] for entry in timesheet_entries if within_dates(i)]

在这里假设within_dates需要一个一天的数字。

现在,要获得总计:

for i in total_times:
   print 'Total for i ', sum(total_times[i])

最新更新