y坐标列表进行排序

y坐标列表进行排序

本文介绍了如何对x-y坐标列表进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要对如下所示的[x,y]坐标列表进行排序:

I need to sort a list of [x,y] coordinates that looks like this:

list = [[1,2],[0,2],[2,1],[1,1],[2,2],[2,0],[0,1],[1,0],[0,0]]

排序后我正在寻找的模式是:

The pattern I'm looking for after sorting is:

[x,y]坐标应首先按y排序,然后按x排序.新列表应如下所示:

[x,y] coordinate shall be sorted by y first and then by x. The new list should look like:

list = [[0,0],[1,0],[2,0],[0,1],[1,1],[2,1],[0,2],[1,2],[2,2]]

我不知道该怎么做,将不胜感激.

I can't figure out how to do it and would appreciate some help.

推荐答案

使用 sorted 与密钥:

use sorted with key:

>>> my_list = [[1,2],[0,2],[2,1],[1,1],[2,2],[2,0],[0,1],[1,0],[0,0]]
>>> sorted(my_list , key=lambda k: [k[1], k[0]])
[[0, 0], [1, 0], [2, 0], [0, 1], [1, 1], [2, 1], [0, 2], [1, 2], [2, 2]]

它将首先对y值进行排序,如果相等,则将对x值进行排序.

It will first sort on the y value and if that's equal then it will sort on the x value.

我还建议不要将list用作变量,因为它是内置数据结构.

I would also advise to not use list as a variable because it is a built-in data structure.

这篇关于如何对x-y坐标列表进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 04:07