如何正确计算给定时间段之间的时间间隔?
How to correctly calculate time intervals between given sections of time?
对于时间跟踪应用程序,一天分为五个部分:
0:00 - 6:00, 6:00 - 14:00, 14:00 - 20:00, 20:00 - 23:00 , 23:00 - (无穷大)
这五个 "bins" 需要根据其中一个花费的时间来填写。
例如,有问题的间隔从 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 ]
我觉得一定有比我想出的更好的方法来做到这一点。它完全是硬编码的,我无法想出构建一些东西来减少代码重复,并且可能更灵活一些,例如定义 bins 的数量及其长度,而不是像那样对它们进行硬编码。
提前致谢!
希望我正确理解了你的问题。 split 小时在列表 splits
中定义,因此它是可配置的:
data = [[ 6.75, 12.5 ], [ 13.5, 18.25 ]]
splits = [0, 6, 14, 20, 23, float('inf')]
def intersection(a, b, c, d):
if a > d or b < c:
return 0 # no intersection
left, right = max(a, c), min(b, d)
return right - left
out = [sum(v) for v in zip(*[[intersection(*i, *s) for s in zip(splits, splits[1::])] for i in data])]
print(out)
打印:
[0, 6.25, 4.25, 0, 0]
对于时间跟踪应用程序,一天分为五个部分:
0:00 - 6:00, 6:00 - 14:00, 14:00 - 20:00, 20:00 - 23:00 , 23:00 - (无穷大)
这五个 "bins" 需要根据其中一个花费的时间来填写。 例如,有问题的间隔从 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 ]
我觉得一定有比我想出的更好的方法来做到这一点。它完全是硬编码的,我无法想出构建一些东西来减少代码重复,并且可能更灵活一些,例如定义 bins 的数量及其长度,而不是像那样对它们进行硬编码。
提前致谢!
希望我正确理解了你的问题。 split 小时在列表 splits
中定义,因此它是可配置的:
data = [[ 6.75, 12.5 ], [ 13.5, 18.25 ]]
splits = [0, 6, 14, 20, 23, float('inf')]
def intersection(a, b, c, d):
if a > d or b < c:
return 0 # no intersection
left, right = max(a, c), min(b, d)
return right - left
out = [sum(v) for v in zip(*[[intersection(*i, *s) for s in zip(splits, splits[1::])] for i in data])]
print(out)
打印:
[0, 6.25, 4.25, 0, 0]