我有两个列表如下:
list_1 = [ [4,9], [10,14], [18,20], [21,23]]
list_2 = [ [3,5], [6,9], [10,11], [12,13], [14,16], [17,23] ]
我需要使用这些规则找到这两者的交集:
答案 0 :(得分:1)
好的,我使用启发式方法得到了解决方案,但我确信它并不漂亮:(
def intersect_windows(list_1, list_2):
if not list_1 or not list_2:
return []
list_1 = [range(x[0], x[1]) for x in list_1]
list_2 = [range(x[0], x[1]) for x in list_2]
result = []
for s1 in list_1:
for s2 in list_2:
intr = [max(s1[0], s2[0]), min(s1[-1], s2[-1])+1]
if (intr[1] - intr[0]) >= 2:
result.append(intr)
return result
##Usage
list_1 = [ [4,9], [10,14], [18,20], [21,23]]
list_2 = [ [3,5], [6,9], [10,11], [12,13], [14,16], [17,23] ]
intersect_windows(list_1, list_2)
[[6, 9], [18, 20], [21, 23]]
答案 1 :(得分:-3)
按照你的定义,广义上的交叉点是我得到的:
list_1 = [ [4,9], [10,14], [18,20], [21,23]]
list_2 = [ [3,5], [6,9], [10,11], [12,13], [14,16], [17,23] ]
list_range1 = [item for sublist in [range(a, b+1) for a,b in list_1] for item in sublist]
list_range2 = [item for sublist in [range(a, b+1) for a,b in list_2] for item in sublist]
c = list(set(list_range1) & set(list_range2))
c.sort()
ans1 = [a for a in list_1 if (a[0] in c and a[1] in c)] + [a for a in list_2 if (a[0] in c and a[1] in c)]
ans2 = [a for a in ans1 if not a[0] == a[1]-1 ]
如果正确(不确定我不确定我的问题),你的例子的答案也应该包含
[[4, 9], [10, 14]]
如同列表2 [3,5],[6,9]与[3,9]无法区分。 如果要区分它们,则应对每个列表的每个第二项执行-1,然后添加+1。