我正在使用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)],然后循环计算时间,并对每一天执行相同的操作。
发布于 2012-10-30 18:34:22
现在还不清楚你想要如何浏览结果。假设您想要统计一周中每天工作的总小时数(时间),请尝试这种方法。
由于星期几是固定的,所以只需循环遍历星期几即可。对于每一天,筛选当天的条目,然后收集时间。您可以使用关键字为星期几的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])https://stackoverflow.com/questions/13136777
复制相似问题