Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 在Python中测试重叠时间_Python 3.x_Time - Fatal编程技术网

Python 3.x 在Python中测试重叠时间

Python 3.x 在Python中测试重叠时间,python-3.x,time,Python 3.x,Time,我在逐字逐句地关注这篇文章,但与原来的文章相比,我得到了不同的结果,我不知道为什么。我想将成对的时间相互比较,以检查它们在哪里重叠。我正在使用Python 3.6.6 这是我的代码: import datetime from collections import namedtuple from itertools import combinations timesok = [('09:30', '10:00'), ('10:00', '10:30'), ('10:30', '11:00')]

我在逐字逐句地关注这篇文章,但与原来的文章相比,我得到了不同的结果,我不知道为什么。我想将成对的时间相互比较,以检查它们在哪里重叠。我正在使用Python 3.6.6

这是我的代码:

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]
我认为结果应该如下(与上面链接中的原始示例相对应):


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

重叠。秒数不会返回你认为它会返回的值

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

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

你想写的是重叠。总秒数()


你仍然有一个问题,那就是你的时间都必须在同一天,不能跨越午夜。我不认为在这里使用
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))

谢谢,这真的很有帮助。我真的把我的头发扯掉了!实际上,我的数据有这样的时间:
times=[('09:30','00:00'),('07:00','00:30'),('10:30','00:15'),('12:15','13:30',('10:00','11:00')]
因此前三个条目跨越一天以上。我注意到,例如,上面的代码将在前两对中返回false。是否有办法克服这一问题。我正在尝试避免在输入中使用日期,因为我正在查看固定的每日计划(日期与此无关).实现了解决包装问题的自定义解决方案。希望这能有所帮助。
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()
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'))