Python 如何按元组的第一个元素对元组列表进行排序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22876410/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 01:51:44  来源:igfitidea点击:

How to sort a list of tuples by their first element?

pythonlistsortingtuples

提问by Ericson Willians

I have a list of tuples:

我有一个元组列表:

self.gridKeys = self.gridMap.keys() # The keys of the instance of the GridMap (It returns the product of every possible combination of positions in the specified grid, in tuples.)
print self.gridKeys

self.gridKeys:

self.gridKeys:

[(7, 3), (6, 9), (0, 7), (1, 6), (3, 7), (2, 5), (8, 5), (5, 8), (4, 0), (9, 0), (6, 7), (5, 5), (7, 6), (0, 4), (1, 1), (3, 2), (2, 6), (8, 2), (4, 5), (9, 3), (6, 0), (7, 5), (0, 1), (3, 1), (9, 9), (7, 8), (2, 1), (8, 9), (9, 4), (5, 1), (7, 2), (1, 5), (3, 6), (2, 2), (8, 6), (4, 1), (9, 7), (6, 4), (5, 4), (7, 1), (0, 5), (1, 0), (0, 8), (3, 5), (2, 7), (8, 3), (4, 6), (9, 2), (6, 1), (5, 7), (7, 4), (0, 2), (1, 3), (4, 8), (3, 0), (2, 8), (9, 8), (8, 0), (6, 2), (5, 0), (1, 4), (3, 9), (2, 3), (1, 9), (8, 7), (4, 2), (9, 6), (6, 5), (5, 3), (7, 0), (6, 8), (0, 6), (1, 7), (0, 9), (3, 4), (2, 4), (8, 4), (5, 9), (4, 7), (9, 1), (6, 6), (5, 6), (7, 7), (0, 3), (1, 2), (4, 9), (3, 3), (2, 9), (8, 1), (4, 4), (6, 3), (0, 0), (7, 9), (3, 8), (2, 0), (1, 8), (8, 8), (4, 3), (9, 5), (5, 2)]

After sorting:

排序后:

self.gridKeys = self.gridMap.keys() # The keys of the instance of the GridMap (It returns the product of every possible combination of positions in the specified grid, in tuples.)
self.gridKeys.sort() # They're dicts, so they need to be properly ordered for further XML-analysis.
print self.gridKeys

self.gridKeys:

self.gridKeys:

[(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (0, 7), (0, 8), (0, 9), (1, 0), (1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9), (2, 0), (2, 1), (2, 2), (2, 3), (2, 4), (2, 5), (2, 6), (2, 7), (2, 8), (2, 9), (3, 0), (3, 1), (3, 2), (3, 3), (3, 4), (3, 5), (3, 6), (3, 7), (3, 8), (3, 9), (4, 0), (4, 1), (4, 2), (4, 3), (4, 4), (4, 5), (4, 6), (4, 7), (4, 8), (4, 9), (5, 0), (5, 1), (5, 2), (5, 3), (5, 4), (5, 5), (5, 6), (5, 7), (5, 8), (5, 9), (6, 0), (6, 1), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6), (6, 7), (6, 8), (6, 9), (7, 0), (7, 1), (7, 2), (7, 3), (7, 4), (7, 5), (7, 6), (7, 7), (7, 8), (7, 9), (8, 0), (8, 1), (8, 2), (8, 3), (8, 4), (8, 5), (8, 6), (8, 7), (8, 8), (8, 9), (9, 0), (9, 1), (9, 2), (9, 3), (9, 4), (9, 5), (9, 6), (9, 7), (9, 8), (9, 9)]

The first element of each tuple is the "x", and the second the "y". I'm moving objects in a list through iteration and using these keys (So, if I want to move something in the x axis, I have to go through all the column, and that might be causing a horrid problem that I'm not being able to solve).

每个元组的第一个元素是“x”,第二个元素是“y”。我正在通过迭代和使用这些键移动列表中的对象(所以,如果我想在 x 轴上移动某些东西,我必须遍历所有列,这可能会导致一个可怕的问题,我不是能够解决)。

How can I sort the tuples in this way?:

如何以这种方式对元组进行排序?:

[(1, 0), (2, 0), (3, 0), (4, 0), (5, 0), ...]

采纳答案by thefourtheye

You can use the keyparameter of the sortfunction, to sort the tuples. The function of keyparameter, is to come up with a value which has to be used to compare two objects. So, in your case, if you want the sortto use only the first element in the tuple, you can do something like this

您可以使用函数的key参数对sort元组进行排序。key参数的功能是提出一个必须用来比较两个对象的值。因此,在您的情况下,如果您只想sort使用元组中的第一个元素,您可以执行以下操作

self.gridKeys.sort(key=lambda x: x[0])

If you want to use only the second element in the tuple, then

如果只想使用元组中的第二个元素,则

self.gridKeys.sort(key=lambda x: x[1])

sortfunction will pass each and every element in the list to the lambda function you pass as parameter to keyand it will use the value it returns, to compare two objects in the list. So, in your case, lets say you have two items in the list like this

sort函数会将列表中的每个元素传递给您作为参数传递给的 lambda 函数key,它将使用它返回的值来比较列表中的两个对象。所以,在你的情况下,假设你在列表中有两个这样的项目

data = [(1, 3), (1, 2)]

and if you want to sort by the second element, then you would do

如果你想按第二个元素排序,那么你会这样做

data.sort(key=lambda x: x[1])

First it passes (1, 3)to the lambda function which returns the element at index 1, which is 3and that will represent this tuple during the comparison. The same way, 2will be used for the second tuple.

第一它传递(1, 3)到在索引返回元素lambda函数1,这是3与将在比较期间表示该元组。同样的方式,2将用于第二个元组。

回答by inspectorG4dget

This should do the trick

这应该可以解决问题

import operator
self.gridKeys.sort(key=operator.itemgetter(1))

回答by Nuclearman

While thefourtheye's solution is correct in the strict sense that it is exactly what you asked for in the title. It may not be actually what you want. It may be better to take it a bit farther via sorting by the reverse of the tuple instead.

虽然 thefourtheye 的解决方案在严格意义上是正确的,但它正是您在标题中所要求的。它实际上可能不是您想要的。最好通过按元组的反向排序来更远一点。

self.gridKeys.sort(key=lambda x:tuple(reversed(x)))

This forces you to have an ordering like:

这会迫使您进行如下排序:

[(0, 0), (1, 0), (2, 0), (3, 0), (4, 0), ...]

Rather than having the first element be unordered like:

而不是让第一个元素像这样无序:

[(4, 0), (9, 0), (6, 0), (1, 0), (3, 0), ...]

Which is what I get when using:

这是我在使用时得到的:

self.gridKeys.sort(key=lambda x: x[1])

By default Python does a lexicographical sort from left to right. Reversing the tuple effectively makes Python do the lexicographical sort from right to left.

默认情况下,Python 从左到右进行字典排序。有效地反转元组使 Python 从右到左进行字典排序。