简体   繁体   English

Python:对列表列表进行排序

[英]Python: Sorting a list of lists

I have a list of lists that looks something like this: 我有一个列表列表,看起来像这样:

testList = 
[[(480.0, 349.0), (459.0, 354.0), (467.0, 343.0)] 
[(467.0, 343.0), (455.0, 344.0), (462.0, 334.0)] 
[(459.0, 354.0), (480.0, 349.0), (483.0, 366.0)] ...]

Each list represents triangles that I am trying to draw with a line. 每个列表代表我要用线绘制的三角形。 I have an unrelated drawing method that draws a line from first vertex to second to third and back to first vertex (thereby drawing a triangle). 我有一个不相关的绘制方法,该方法从第一个顶点到第二个到第三个顶点再回到第一个顶点绘制一条线(从而绘制一个三角形)。 I also want to connect these triangles together without drawing new lines, and so I want to consecutively sequence the testList[i][0]s. 我也想将这些三角形连接在一起而不画新线,因此我想连续地对testList [i] [0] s进行排序。

And so, I would like to reorder the vertices within the list AND testList , to something like this: 因此,我想对列表AND testList的顶点重新排序,如下所示:

testList = 
[[(480.0, 349.0), (459.0, 354.0), (467.0, 343.0)] 
[(480.0, 349.0), (459.0, 354.0),  (483.0, 366.0)] 
[(467.0, 343.0), (455.0, 344.0), (462.0, 334.0)]...]

where testList[2] jumps up to testList[1] , and testList[1][0] and testList[1][1] switch. 其中testList[2]跳至testList[1] ,然后testList[1][0]testList[1][1]切换。

Thanks to Alfonso Jiménez , here is the approach I have so far: 感谢AlfonsoJiménez ,这是我到目前为止的方法:

for subList in testList1:
    subList.sort()
testList1.sort()

which works pretty well, except I still do notice in the complete testList parts like: 效果很好,除了我仍然在完整的testList部分中注意到:

...[(215.0, 737.0), (298.0, 633.0), (317.0, 660.0)]
[(317.0, 660.0), (333.0, 599.0), (394.0, 660.0)]
[(298.0, 633.0), (317.0, 660.0), (333.0, 599.0)]
[(282.0, 75.0), (316.0, 74.0), (336.0, 71.0)]...

that could be rearranged to have identical testList[i][0s] but don't (notice the (317.0, 660.0) vertex not moving to position [0]. You may see the complete output here 可以重新排列为具有相同的testList [i] [0s],但不会(请注意(317.0,660.0)顶点不会移动到位置[0]。您可能会在此处看到完整的输出

I am a beginner in python so any help would be incredibly appreciated. 我是python的初学者,所以任何帮助都将不胜感激。 Thank you! 谢谢!

You question is little confusing, is this what you want : 您的问题有点令人困惑,这是您想要的:

I tried recursive approach , it will chuck and reorder 3-3 items from your list : 我尝试了递归方法,它将从您的列表中删除并重新排序3-3个项目:

testList = [[(480.0, 349.0), (459.0, 354.0), (467.0, 343.0)],
[(467.0, 343.0), (455.0, 344.0), (462.0, 334.0)],
[(459.0, 354.0), (480.0, 349.0), (483.0, 366.0)]]
def  triangles(test_line):
    updated_list=[]
    if not test_line:
        return 0
    else:

        for index,item in enumerate(test_line[:3]):
            if index==0:
                updated_list.insert(index,test_line[index])
            if index==1:


                updated_list.insert(2,test_line[index])
            if index==2:
                test_line[index][0], test_line[index][1] = test_line[index][1], test_line[index][0]

                updated_list.insert(1,test_line[index])


        print(updated_list)
        return triangles(test_line[3:])

print(triangles(testList))

output: 输出:

[[(480.0, 349.0), (459.0, 354.0), (467.0, 343.0)], [(480.0, 349.0), (459.0, 354.0), (483.0, 366.0)], [(467.0, 343.0), (455.0, 344.0), (462.0, 334.0)]]

Test case with your updated long list: 具有更新后的长列表的测试用例:

testList = [[(480.0, 349.0), (459.0, 354.0), (467.0, 343.0)],
[(467.0, 343.0), (455.0, 344.0), (462.0, 334.0)],
[(459.0, 354.0), (480.0, 349.0), (483.0, 366.0)],
[(215.0, 737.0), (298.0, 633.0), (317.0, 660.0)],
[(317.0, 660.0), (333.0, 599.0), (394.0, 660.0)],
[(298.0, 633.0), (317.0, 660.0), (333.0, 599.0)]]

output: 输出:

[[(480.0, 349.0), (459.0, 354.0), (467.0, 343.0)], [(480.0, 349.0), (459.0, 354.0), (483.0, 366.0)], [(467.0, 343.0), (455.0, 344.0), (462.0, 334.0)]]
[[(215.0, 737.0), (298.0, 633.0), (317.0, 660.0)], [(317.0, 660.0), (298.0, 633.0), (333.0, 599.0)], [(317.0, 660.0), (333.0, 599.0), (394.0, 660.0)]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM