如果他们有共同的段,加入行列表?
joining list of lines if they have mutual segments?
我有行列表,每一行都是这样的列表
[firstpoint, secondpoint]
例如我有 4 行是这样的:
listoflines = [[0,2],[1,3],[2,5],[6,7]]
所以当你看到一些线有相互段时,我想要的是如果它们有相互段就加入它们,因此结果会是这样的:
newlistoflines = [[0,5],[6,7]]
我尝试的是使用一个函数将它们相互比较并循环遍历我的行列表,但我对其结果有疑问:
def JOINLINES(line1, line2):
x1 = line1[0]
x2 = line1[1]
x3 = line2[0]
x4 = line2[1]
if x2 < x3:
result = (x1, x2), (x3, x4)
else:
result = (min(x1, x2, x3, x4), max(x1, x2, x3, x4))
return result
newbeamlist = []
for i in range(1, len(beams)):
newbeamlist.append(JOINLINES(beams[i - 1], beams[i]))
输出=[(0, 3), (1, 5), ((2, 5), (6, 7))]
您的解决方案是附加 JOINLINES 函数的结果。
不考虑下一条也可以是互段
试试这个。
我假设了一些事情。
# assuming that the items in the list are ordered according to the first item in each sublist.
# This will enable us to find mutual segments (overlaps) easily
# if the list is not sorted, then we can sort it using the first item in each sublist item
li = [[0,2],[1,3],[2,5],[6,7], [10,11],[7,9]]
# sorting using the first item in each sublist item
li.sort(key=lambda x: x[0])
# out: [[0,5],[6,7]]
newli= []
left_prev = li[0][0]
right_prev = li[0][1]
for i in range(1,len(li)):
left_current = li[i][0]
right_current = li[i][1]
if left_current <= right_prev:
# mutual segment (overlap) found
right_prev = right_current
continue
else:
# no mutual segment(overlap), append the previous to the new list
newli.append([left_prev, right_prev])
left_prev = left_current
right_prev = right_current
# appending the last item
newli.append([left_prev, right_prev])
print(newli) # [[0, 5], [6, 9], [10, 11]]
我有行列表,每一行都是这样的列表
[firstpoint, secondpoint]
例如我有 4 行是这样的:
listoflines = [[0,2],[1,3],[2,5],[6,7]]
所以当你看到一些线有相互段时,我想要的是如果它们有相互段就加入它们,因此结果会是这样的:
newlistoflines = [[0,5],[6,7]]
我尝试的是使用一个函数将它们相互比较并循环遍历我的行列表,但我对其结果有疑问:
def JOINLINES(line1, line2):
x1 = line1[0]
x2 = line1[1]
x3 = line2[0]
x4 = line2[1]
if x2 < x3:
result = (x1, x2), (x3, x4)
else:
result = (min(x1, x2, x3, x4), max(x1, x2, x3, x4))
return result
newbeamlist = []
for i in range(1, len(beams)):
newbeamlist.append(JOINLINES(beams[i - 1], beams[i]))
输出=[(0, 3), (1, 5), ((2, 5), (6, 7))]
您的解决方案是附加 JOINLINES 函数的结果。
不考虑下一条也可以是互段
试试这个。 我假设了一些事情。
# assuming that the items in the list are ordered according to the first item in each sublist.
# This will enable us to find mutual segments (overlaps) easily
# if the list is not sorted, then we can sort it using the first item in each sublist item
li = [[0,2],[1,3],[2,5],[6,7], [10,11],[7,9]]
# sorting using the first item in each sublist item
li.sort(key=lambda x: x[0])
# out: [[0,5],[6,7]]
newli= []
left_prev = li[0][0]
right_prev = li[0][1]
for i in range(1,len(li)):
left_current = li[i][0]
right_current = li[i][1]
if left_current <= right_prev:
# mutual segment (overlap) found
right_prev = right_current
continue
else:
# no mutual segment(overlap), append the previous to the new list
newli.append([left_prev, right_prev])
left_prev = left_current
right_prev = right_current
# appending the last item
newli.append([left_prev, right_prev])
print(newli) # [[0, 5], [6, 9], [10, 11]]