1

对于时间跟踪应用程序,一天分为五个部分:

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 ]

我觉得必须有比我想出的更好的方法来做到这一点。它完全是硬编码的,我无法构建一些东西来减少代码重复,也许更灵活一些,比如定义箱的数量和它们的长度,而不是像那样硬编码它们。

提前致谢!

4

1 回答 1

1

我希望我正确理解了你的问题。拆分时间在list 中定义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]
于 2020-05-06T21:35:12.850 回答