在 Python 中测试重叠时间



我逐字逐句地遵循这个,但与原始帖子相比,我得到了不同的结果,我不知道为什么。我想将成对的时间相互比较,以检查它们重叠的位置。我正在使用Python 3.6.6

查找日期是否在 N 对列表中重叠

这是我编码的内容:

import datetime
from collections import namedtuple
from itertools import combinations
timesok = [('09:30', '10:00'), ('10:00', '10:30'), ('10:30', '11:00')]
wrongtimes1 = [('9:30', '10:00'), ('9:00', '10:30'), ('10:30', '11:00')]
wrongtimes2=[('9:30', '10:00'), ('10:00', '10:30'), ('9:15', '9:45')]
def test_overlap(dt1_st, dt1_end, dt2_st, dt2_end):
    Range = namedtuple('Range', ['start', 'end'])
    r1 = Range(start=dt1_st, end=dt1_end)
    r2 = Range(start=dt2_st, end=dt2_end)
    latest_start = max(r1.start, r2.start)
    earliest_end = min(r1.end, r2.end)
    overlap = (earliest_end - latest_start)
    return overlap.seconds
def find_overlaps(times):
    pairs = list(combinations(times, 2))
    print(pairs)
    for pair in pairs:
        start1 = datetime.datetime.strptime(pair[0][0], '%H:%M')
        end1   = datetime.datetime.strptime(pair[0][1], '%H:%M')
        start2 = datetime.datetime.strptime(pair[1][0], '%H:%M')
        end2   = datetime.datetime.strptime(pair[1][1], '%H:%M')
        yield test_overlap(start1, end1, start2, end2) > 0
list(find_overlaps(timesok))
list(find_overlaps(wrongtimes1))
list(find_overlaps(wrongtimes2))
 # timesok result:
list(find_overlaps(timesok))
[(('09:30', '10:00'), ('10:00', '10:30')), 
 (('09:30', '10:00'), ('10:30', '11:00')), 
 (('10:00', '10:30'), ('10:30', '11:00'))]
Out[7]: [False, True, False]
 # wrongtimes1 result:
list(find_overlaps(wrongtimes1))
[(('9:30', '10:00'), ('9:00', '10:30')), 
 (('9:30', '10:00'), ('10:30', '11:00')),
 (('9:00', '10:30'), ('10:30', '11:00'))]
Out[8]: [True, True, False]
 # wrongtimes2 result:
list(find_overlaps(wrongtimes2))
[(('9:30', '10:00'), ('10:00', '10:30')), 
 (('9:30', '10:00'), ('9:15', '9:45')), 
 (('10:00', '10:30'), ('9:15', '9:45'))]
Out[9]: [False, True, True]

我认为结果应该如下(与上面链接中的原始示例相对应(:

Out[7]: [False, False, False]
Out[8]: [True, False, False]
Out[9]: [False, True, False]

我在这里错过了一些非常明显的东西吗?我是一个完全的Python新手,所以对我温柔(!

overlap.seconds不会返回您认为它的作用。

overlapdatetime减法的结果,即datetime.timedelta .

timedelta.seconds 返回总秒数。它返回增量的第二部分,如果 timedelta 为 -1 day, 23:45:00,则 timedelta.seconds 为 85500

你想写的是overlap.total_seconds()

def test_overlap(dt1_st, dt1_end, dt2_st, dt2_end):
    Range = namedtuple('Range', ['start', 'end'])
    r1 = Range(start=dt1_st, end=dt1_end)
    r2 = Range(start=dt2_st, end=dt2_end)
    latest_start = max(r1.start, r2.start)
    earliest_end = min(r1.end, r2.end)
    overlap = (earliest_end - latest_start)
    return overlap.total_seconds()

你仍然有一个问题,你的时间都必须在同一天,不能跨越午夜。我不认为在这里使用datetime是正确的方法,我认为编写自定义代码更容易。

给你:

from itertools import combinations
def test_overlap(t1_st, t1_end, t2_st, t2_end):
    def convert_to_minutes(t_str):
        hours, minutes = t_str.split(':')
        return 60*int(hours)+int(minutes)
    t1_st = convert_to_minutes(t1_st)
    t1_end = convert_to_minutes(t1_end)
    t2_st = convert_to_minutes(t2_st)
    t2_end = convert_to_minutes(t2_end)
    # Check for wrapping time differences
    if t1_end < t1_st:
        if t2_end < t2_st:
            # Both wrap, therefore they overlap at midnight
            return True
        # t2 doesn't wrap. Therefore t1 has to start after t2 and end before
        return t1_st < t2_end or t2_st < t1_end
    if t2_end < t2_st:
        # only t2 wraps. Same as before, just reversed
        return t2_st < t1_end or t1_st < t2_end
    # They don't wrap and the start of one comes after the end of the other,
    # therefore they don't overlap
    if t1_st >= t2_end or t2_st >= t1_end:
        return False
    # In all other cases, they have to overlap
    return True
times = [('09:30', '00:00'), ('07:00', '00:30'), ('10:30', '00:15'), ('12:15', '13:30'), ('10:00', '11:00'), ('00:15', '01:15')]
pairs = list(combinations(times, 2))
for pair in pairs:
    start1 = pair[0][0]
    end1   = pair[0][1]
    start2 = pair[1][0]
    end2   = pair[1][1]
    print(str(test_overlap(start1, end1, start2, end2)) + "t" + str(pair))
True    (('09:30', '00:00'), ('07:00', '00:30'))
True    (('09:30', '00:00'), ('10:30', '00:15'))
True    (('09:30', '00:00'), ('12:15', '13:30'))
True    (('09:30', '00:00'), ('10:00', '11:00'))
False   (('09:30', '00:00'), ('00:15', '01:15'))
True    (('07:00', '00:30'), ('10:30', '00:15'))
True    (('07:00', '00:30'), ('12:15', '13:30'))
True    (('07:00', '00:30'), ('10:00', '11:00'))
True    (('07:00', '00:30'), ('00:15', '01:15'))
True    (('10:30', '00:15'), ('12:15', '13:30'))
True    (('10:30', '00:15'), ('10:00', '11:00'))
False   (('10:30', '00:15'), ('00:15', '01:15'))
False   (('12:15', '13:30'), ('10:00', '11:00'))
False   (('12:15', '13:30'), ('00:15', '01:15'))
False   (('10:00', '11:00'), ('00:15', '01:15'))

最新更新