合并具有重叠时间范围的时间范围元组列表

Pra*_*ota 29 python algorithm merge

我有一个元组列表,每个元组都是一个元组(start-time, end-time).我正在尝试合并所有重叠的时间范围并返回不同时间范围的列表.例如

[(1, 5), (2, 4), (3, 6)] --->  [(1,6)]
[(1, 3), (2, 4), (5, 8)] --->  [(1, 4), (5,8)]
Run Code Online (Sandbox Code Playgroud)

这是我实现它的方式.

# Algorithm
# initialranges: [(a,b), (c,d), (e,f), ...]
# First we sort each tuple then whole list.
# This will ensure that a<b, c<d, e<f ... and a < c < e ... 
# BUT the order of b, d, f ... is still random
# Now we have only 3 possibilities
#================================================
# b<c<d: a-------b           Ans: [(a,b),(c,d)]
#                  c---d
# c<=b<d: a-------b          Ans: [(a,d)]
#               c---d
# c<d<b: a-------b           Ans: [(a,b)]
#         c---d
#================================================
def mergeoverlapping(initialranges):
    i = sorted(set([tuple(sorted(x)) for x in initialranges]))

    # initialize final ranges to [(a,b)]
    f = [i[0]]
    for c, d in i[1:]:
        a, b = f[-1]
        if c<=b<d:
            f[-1] = a, d
        elif b<c<d:
            f.append((c,d))
        else:
            # else case included for clarity. Since 
            # we already sorted the tuples and the list
            # only remaining possibility is c<d<b
            # in which case we can silently pass
            pass
    return f
Run Code Online (Sandbox Code Playgroud)

我想知道是否

  1. 在某些python模块中是一个内置函数,可以更有效地执行此操作吗?要么
  2. 是否有更多的pythonic方式来实现相同的目标?

非常感谢您的帮助.谢谢!

sam*_*ias 16

Pythonic提供了一些提高效率的方法:

  1. 消除set()构造,因为算法应该在主循环期间删除重复.
  2. 如果您只需要迭代结果,请使用yield生成值.
  3. 减少中间对象的构造,例如:将tuple()调用移动到产生最终值的位置,从而使您不必构造和丢弃额外的元组,并重用列表saved来存储当前时间范围以进行比较.

码:

def merge(times):
    saved = list(times[0])
    for st, en in sorted([sorted(t) for t in times]):
        if st <= saved[1]:
            saved[1] = max(saved[1], en)
        else:
            yield tuple(saved)
            saved[0] = st
            saved[1] = en
    yield tuple(saved)

data = [
    [(1, 5), (2, 4), (3, 6)],
    [(1, 3), (2, 4), (5, 8)]
    ]

for times in data:
    print list(merge(times))
Run Code Online (Sandbox Code Playgroud)

  • 不幸的是,如果`len(times)== 0`,则会失败. (2认同)