如何将重叠范围“联合”到非重叠范围?

时间:2018-10-18 18:01:08

标签: python range aggregate union overlap

问题: 谁能建议一种更好或更Python的方法,将重叠范围对减少为非重叠范围对?

背景: 我有一个代表开始和结束对的元组列表。我试图从根本上完成所有起点对的结合。输入起始端对具有重叠的值,并且输出应表示输入起始端对而没有任何重叠。

下面的代码很接近,但是有错误,因为它输出的额外范围不在输入中(我也意识到它不是很好,以及为什么它是错误的)。谁能建议一个更好的方法,或者一些我忽略的内置函数?

道歉的基本问题。 感谢您的帮助!

##create example data
pairA =[(0,5),(10,12)]
pairB =[(1,2),(11,15)]
pairC =[(1,4),(10,12),(15,17)]

#combine the lists to one list
#ultimately may have n number of lists and felt it would be easier to
merged = pairA + pairB +pairC
# produce union of list * unpacks the arguments of a list
listUnion= sorted(set().union(*merged))

#this is the piece of code I am looking at improving
#it creates new start end pairs based on the union
lastElement =listUnion[-1]
outList=[]

for item in listUnion:
    #create start end pair from value i and i+1
    if item != lastElement:
        outList.append((item,listUnion[listUnion.index(item)+1]))
    else:
        #last element of the list, becomes the last element of list pair
        #it can be ignored
        pass
print outList 
"""output: [(0, 1), (1, 2), (2,4), (4, 5), (5, 10), (10, 11), (11, 12), (12, 15), (15, 
17)]
correct output: would not have (5,10) as there is no overlap here in the input """

编辑:添加了问题的直观表示enter image description here

3 个答案:

答案 0 :(得分:1)

这是一个解决方案。这可能不是很Python,因为我对Python的经验非常有限,但是可以。

pairs_a = [(0, 5), (10, 12)]
pairs_b = [(1, 2), (11, 15)]
pairs_c = [(1, 4), (10, 12), (15, 17)]

merged = pairs_a + pairs_b + pairs_c
merged.sort()

set_list = []
cur_set = set()
cur_max = merged[0][1]
for pair in merged:
    p0, p1 = pair
    if cur_max < p0:
        set_list.append(cur_set)
        cur_set = set()
    cur_set.add(p0)
    cur_set.add(p1)
    if cur_max < p1:
        cur_max = p1
set_list.append(cur_set)

out_list = []
for my_set in set_list:
    my_list = sorted(my_set)
    p0 = my_list[0]
    for p1 in my_list[1:]:
        out_list.append((p0, p1))
        p0 = p1

# more pythonic but less readable in spite of indentation efforts:
# out_list = [pair
#             for zipped in [zip(list[:-1], list[1:])
#                            for list in [sorted(set)
#                                         for set in set_list]]
#                 for pair in zipped]

# alternate ending:
# out_list = [sorted(set) for set in set_list]

print(out_list)

这个想法是按照第一项对所有范围对进行排序。 merged.sort()就是这样做的(它使用连续的元组成员来消除歧义,但这在这里并不重要)。然后,我们遍历已排序的范围对,并且只要我们处于一堆重叠范围之内,就可以将所有开始和结束添加到当前集合中。为了知道束束何时结束,我们保留所有范围末端的最大值。一旦超出此最大值的范围开始到达,我们便通过将当前集合附加到列表中来存储它,并开始一个新的集合。循环后,最后一组必须添加到列表中。现在我们有了一个集合列表,我们可以轻松地将其转换为列表列表或成对列表。

答案 1 :(得分:0)

不确定您的环境限制,但是如果您没有任何限制,则可能要考虑以下问题:https://pypi.org/project/intervaltree/ 特别是

result_tree = tree.union(iterable)

答案 2 :(得分:0)

请您澄清一下问题。我看到[(0,5), (1,2)]产生了[(0, 1), (1, 2), (2, 5)][(0,5), (1,5)]会产生什么,[(0, 1), (1, 5), (5, 5)],或者只是[(0,1)],还是其他?