对于时间跟踪应用程序,一天分为五个部分:
0:00 - 6:00, 6:00 - 14:00, 14:00 - 20:00, 20:00 - 23:00, 23:00 - (无穷大)
这五个“垃圾箱”需要根据在其中任何一个中花费的时间来填充。例如,所讨论的时间间隔从 5:00 开始到 16:00 结束,bin #1 包含 1 小时,bin #2 包含 8 小时,bin #3 包含 2 小时,bin #4 包含 0 小时,bin #5 包含0小时。超过 23:00 的任何时间都进入 bin #5。
到目前为止,我想出了这个:
sections = [ 0, 0, 0, 0, 0 ]
for tframe in numericdata:
if tframe[0] < 6.00: # starts before 6:00
if tframe[1] >= 6.00: # ends after 6:00
sections[0] += 6.00 - tframe[0]
else: # ends before 6:00
sections[0] += tframe[1] - tframe[0]
continue
if tframe[1] >= 14.00: # ends after 14:00
sections[1] += 14.00 - 6.00
else: # ends between 6:00 and 14:00
sections[1] += tframe[1] - 6.00
continue
if tframe[1] >= 20.00: # ends after 20:00
sections[2] += 20.00 - 14.00
else: # ends between 14:00 and 20:00
sections[2] += tframe[1] - 14.00
continue
if tframe[1] >= 23.00: # ends after 23:00
sections[3] += 23.00 - 20.00
sections[4] += tframe[1] - 23.00
else: # ends between 20:00 and 23:00
sections[3] += tframe[1] - 20.00
continue
elif tframe[0] < 14.00: # starts between 6:00 and 14:00
if tframe[1] >= 14.00: # ends after 14:00
sections[1] += 14.00 - tframe[0]
else: # ends before 14:00
sections[1] += tframe[1] - tframe[0]
continue
if tframe[1] >= 20.00: # ends after 20:00
sections[2] += 20.00 - 14.00
else: # ends between 14:00 and 20:00
sections[2] += tframe[1] - 14.00
continue
if tframe[1] >= 23.00: # ends after 23:00
sections[3] += 23.00 - 20.00
sections[4] += tframe[1] - 23.00
else: # ends between 20:00 and 23:00
sections[3] += tframe[1] - 20.00
continue
elif tframe[0] < 20.00: # starts between 14:00 and 20:00
if tframe[1] >= 20.00: # ends after 20:00
sections[2] += 20.00 - tframe[0]
else: # ends before 20:00
sections[2] += tframe[1] - tframe[0]
continue
if tframe[1] >= 23.00: # ends after 23:00
sections[3] += 23.00 - 20.00
sections[4] += tframe[1] - 23.00
else: # ends between 20:00 and 23:00
sections[3] += tframe[1] - 20.00
continue
elif tframe[0] < 23.00: # starts between 20:00 and 23:00
if tframe[1] >= 23.00: # ends after 23:00
sections[3] += 23.00 - tframe[0]
sections[4] += tframe[1] - 23.00
else: # ends before 23:00
sections[3] += tframe[1] - tframe[0]
continue
else: # starts and ends some time after 23:00
sections[4] += tframe[1] - tframe[0]
numericdata
是一个数组,包含间隔作为开始和结束时间的元组。所有时间值都已转换为带分数的小时,因此 13:15 被编码为 13.25 等。例如,numericdata
可能包含[ [ 6.75, 12.5 ], [ 13.5, 18.25 ] ]
,因此两个时间间隔,一个从 6:45 到 12:30,另一个从 13:30 到18:15。结果sections
数组如下所示:[ 0, 6.25, 4.25, 0, 0 ]
我觉得必须有比我想出的更好的方法来做到这一点。它完全是硬编码的,我无法构建一些东西来减少代码重复,也许更灵活一些,比如定义箱的数量和它们的长度,而不是像那样硬编码它们。
提前致谢!