加入线条列表,如果它们有相互段?
我有行列表,每行都是这样的列表,
[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]))
output = [(0,3),(1,5),(( (2,5),(6,7)]
i have list of lines , each line is a list like this
[firstpoint, secondpoint]
for example i have 4 lines like this:
listoflines = [[0,2],[1,3],[2,5],[6,7]]
so as you see some lines have mutual segments what i want is to join them if they have mutual segments therefore the result would be like:
newlistoflines = [[0,5],[6,7]]
what i have tried is to use a function to compare them with each other and loop over my list of lines but i have problem with the its result:
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]))
output = [(0, 3), (1, 5), ((2, 5), (6, 7))]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的解决方案正在附加Joinlines功能的结果。
这不是考虑下一个项目也可能是相互段。
试试看。
我已经假设了几件事。
Your solution is appending the result of the JOINLINES function.
It is not considering that the next item could also be a mutual segment.
Try this instead.
I have assumed a few things.