简体   繁体   English

如何对 python 中的二维列表进行排序?

[英]How do you sort a 2D List in python?

I have a list of co-ordinates which indicate the top-left position (first two values) and the bottom right co-ordinates (the last two) of a detected word in an image我有一个坐标列表,指示图像中检测到的单词的左上角 position (前两个值)和右下角坐标(最后两个)

It looks something like this:它看起来像这样:

boxes= [[ 27,  22,  84 , 54],
     [261 ,127 ,294 ,163],
     [224 , 21 ,279 , 54],
     [ 45  ,83 , 96 ,111],
     [ 10 ,126 , 66 ,163],
     [189 ,185 ,219 ,222],
     [154 , 21 ,192 , 59],
     [278 , 25 ,318 , 58],
     [146 ,188 ,186, 220]]

so in boxes[0] would give me [27,22,84,54] and lets call them startX, startY, endX, endY respectively所以在 box[0] 中会给我 [27,22,84,54] 并让它们分别称为 startX、startY、endX、endY

For visualizing:对于可视化:

 (27,22)------------.
    '               '
    '               '
    '               '
    '---------------(84,54)

I need to sort this in a way that startY begins being minimum with increasing startX and startY incresaing我需要以 startY 随着 startX 和 startY 增加而开始最小化的方式进行排序

so the output must look something like this所以 output 必须看起来像这样

     [[154 , 21 ,192 , 59],
     [224 , 21 ,279 , 54],
     [ 27,  22,  84 , 54],
     [278 , 25 ,318 , 58],
     [ 45  ,83 , 96 ,111],
     [ 10 ,126 , 66 ,163],
     [261 ,127 ,294 ,163],
     [189 ,185 ,219 ,222],
     [146 ,188 ,186, 220]]

Python will sort a tuple based on position, so you can pass a key to sorted() that puts the elements in the order you want to base the sort on: Python 将根据 position 对元组进行排序,因此您可以将key传递给sorted() ,将元素按照您希望排序的顺序排列:

boxes= [[ 27,  22,  84 , 54],
     [261 ,127 ,294 ,163],
     [224 , 21 ,279 , 54],
     [ 45  ,83 , 96 ,111],
     [ 10 ,126 , 66 ,163],
     [189 ,185 ,219 ,222],
     [154 , 21 ,192 , 59],
     [278 , 25 ,318 , 58],
     [146 ,188 ,186, 220]]

sorted(boxes, key=lambda x: (x[1], x[0], x[2], x[3]))

Result:结果:

[[154, 21, 192, 59],
 [224, 21, 279, 54],
 [27, 22, 84, 54],
 [278, 25, 318, 58],
 [45, 83, 96, 111],
 [10, 126, 66, 163],
 [261, 127, 294, 163],
 [189, 185, 219, 222],
 [146, 188, 186, 220]]

If you want to sort the list in-place use boxes.sort() with the same key.如果要对列表进行就地排序,请使用具有相同键的boxes.sort()

OK, so what you want is to first sort for startX and then for startY, while respecting the relative order between elements in startX.好的,所以你想要的是首先对 startX 排序,然后对 startY 排序,同时尊重 startX 中元素之间的相对顺序。 It would look like this:它看起来像这样:

# Input:
[[ 27,  22,  84 , 54],
 [261 ,127 ,294 ,163],
 [224 , 21 ,279 , 54],
 [ 45  ,83 , 96 ,111],
 [ 10 ,126 , 66 ,163],
 [189 ,185 ,219 ,222],
 [154 , 21 ,192 , 59],
 [278 , 25 ,318 , 58],
 [146 ,188 ,186, 220]]
# First sort:
[[ 10 ,126 , 66 ,163],
 [ 27,  22,  84 , 54],
 [ 45  ,83 , 96 ,111],
 [146 ,188 ,186, 220],
 [154 , 21 ,192 , 59],
 [189 ,185 ,219 ,222],
 [224 , 21 ,279 , 54],
 [261 ,127 ,294 ,163],
 [278 , 25 ,318 , 58]]
# Second sort:
[[154 , 21 ,192 , 59],
 [224 , 21 ,279 , 54],
 [ 27,  22,  84 , 54],
 [278 , 25 ,318 , 58],
 [ 45  ,83 , 96 ,111],
 [ 10 ,126 , 66 ,163],
 [261 ,127 ,294 ,163],
 [146 ,188 ,186, 220],
 [189 ,185 ,219 ,222]]

That's what's called a stable sort.这就是所谓的稳定排序。 Now, according to the standard library reference , the sorted function implements a stable sort.现在,根据标准库参考sorted后的 function 实现了稳定排序。

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

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