加入线条列表,如果它们有相互段?

发布于 2025-01-29 06:10:41 字数 758 浏览 2 评论 0原文

我有行列表,每行都是这样的列表,

[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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑梦风尘 2025-02-05 06:10:41

您的解决方案正在附加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]]

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.

# 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]]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文