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

标签 python list sorting 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:

[(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)]

排序后:

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:

[(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)]

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

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

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

最佳答案

您可以使用 sort 函数的 key 参数对元组进行排序。 key 参数的作用是得出一个必须用于比较两个对象的值。因此,在您的情况下,如果您希望 sort 仅使用元组中的第一个元素,您可以这样做

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

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

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

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

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

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

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

首先它将 (1, 3) 传递给 lambda 函数,该函数返回索引 1 处的元素,即 3 并且它将在比较期间代表这个元组。同样,2 将用于第二个元组。

关于python - 如何按第一个元素对元组列表进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22876410/

相关文章:

python - 用条目小部件替换 python tkinter 标签小部件

python - 为什么使用切片复制列表[:] faster than using the obvious way?

sorting - Elasticsearch - 将单个结果推到顶部

C++ 提供了 std::sort,但它是否也提供了高效搜索的功能?

python - 即使在其中的命令退出后继续 for 循环

python - 截断 Pandas 数据框中的所有行

元组相乘时的 Python 语法细节

python - 使用元组 python 列表

r - 如何在R中的list()中滑动元素的长度?

php - 在多维数组中的 php 中排序时忽略重音字符