Python sort() 列表的第一个元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21068315/
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-18 21:58:17  来源:igfitidea点击:

Python sort() first element of list

pythonlistsortingpython-3.3

提问by DaveDave

I have a list that contains non specific amount of elements but every first element of the nested list is an identifier, I would like to use that identifier to sort the list in order

我有一个包含非特定数量元素的列表,但嵌套列表的每个第一个元素都是一个标识符,我想使用该标识符按顺序对列表进行排序

list = [['D', 'F', 'E', 'D', 'F', 'D'],['A', 'F', 'E', 'C', 'F', 'E'],['C', 'E', 'E', 'F', 'E', 'E'],['B', 'F', 'E', 'D', 'F', 'F']]

After its sorted

排序后

list = [['A', 'F', 'E', 'C', 'F', 'E'],['B', 'F', 'E', 'D', 'F', 'F'],['C', 'E', 'E', 'F', 'E', 'E'],['D', 'F', 'E', 'D', 'F', 'D']]

I am using python 3.3.3

我正在使用 python 3.3.3

回答by π?δα? ?κ??

You want to use .sort()or sorted:

您想使用.sort()sorted

>>> t = [['D', 'F', 'E', 'D', 'F', 'D'], ['A', 'F', 'E', 'C', 'F', 'E'], ['C', 'E', 'E', 'F', 'E', 'E'], ['B', 'F', 'E', 'D', 'F', 'F']]
>>> t.sort(key=lambda x: x[0])  # changes the list in-place (and returns None)
>>> t
[['A', 'F', 'E', 'C', 'F', 'E'], ['B', 'F', 'E', 'D', 'F', 'F'], ['C', 'E', 'E', 'F', 'E', 'E'], ['D', 'F', 'E', 'D', 'F', 'D']]

Also note that your list needs commas between its elements. Here is the result for sorted:

另请注意,您的列表需要在其元素之间使用逗号。这是结果sorted

>>> sorted(t)  # does not change the list but returns the sorted list
[['A', 'F', 'E', 'C', 'F', 'E'], ['B', 'F', 'E', 'D', 'F', 'F'], ['C', 'E', 'E', 'F', 'E', 'E'], ['D', 'F', 'E', 'D', 'F', 'D']]

As you can see, the latter example sorts the lists without any key argument. The former example can as well; but you mention that only the first element is a unique identifier, so there is no way to tell what the secondary criteria might be for sorting the list beyond the first element.

如您所见,后一个示例在没有任何键参数的情况下对列表进行排序。前一个例子也可以;但是您提到只有第一个元素是唯一标识符,因此无法确定在第一个元素之外对列表进行排序的次要标准是什么。

回答by Daniil Grankin

Use this

用这个

list.sort(lambda x,y : cmp(x[0], y[0]))

UPDATEIt works for 2.7 but not for Python 3.3

更新它适用于 2.7 但不适用于 Python 3.3

回答by qwr

lists.sort(key = lambda x: x[0])Make sure you put commas between each list in the larger list.

lists.sort(key = lambda x: x[0])确保在较大列表中的每个列表之间放置逗号。

回答by wwii

Essentially the same as the others but uses operator.itemgetter(),

本质上与其他相同,但使用 operator.itemgetter(),

from operator import itemgetter
first_item = itemgetter(0)
new_list = sorted(original_list, key = first_item)

回答by Dan

Python automatically sorts lists of lists by the first element. For example:

Python 自动按第一个元素对列表列表进行排序。例如:

lol=[[1,2,3],[5,6,7],[0,9,9]]
sorted(lol)
[[0, 9, 9], [1, 2, 3], [5, 6, 7]]

回答by Aaron Hall

You shouldn't overwrite the builtin list constructor, list, use another name instead like this:

您不应该覆盖内置列表构造函数, list,而是使用另一个名称,如下所示:

>>> a_list = [['D', 'F', 'E', 'D', 'F', 'D'],['A', 'F', 'E', 'C', 'F', 'E'],['C', 'E', 'E', 'F', 'E', 'E'],['B', 'F', 'E', 'D', 'F', 'F']]

To sort the list in place, use the list.sortmethod:

要对列表进行原地排序,请使用以下list.sort方法:

>>> a_list.sort()

>>> a_list
[['A', 'F', 'E', 'C', 'F', 'E'], ['B', 'F', 'E', 'D', 'F', 'F'], ['C', 'E', 'E', 'F', 'E', 'E'], ['D', 'F', 'E', 'D', 'F', 'D']]

The built-in function, sorted, returns a new list, which is something you didn't seem to want to do. It returns a new list, which if you no longer need the old list would waste space in memory.

内置函数sorted返回一个新列表,这似乎是您不想做的事情。它返回一个新列表,如果您不再需要旧列表,则会浪费内存空间。

Python automatically sorts on the first element. It then automatically sorts on the second, third, and so on. Using lambda as others suggested would mean you would only sort on the first element, and the following elements would be ignored.

Python 自动对第一个元素进行排序。然后它会自动对第二个、第三个等进行排序。按照其他人的建议使用 lambda 意味着您只会对第一个元素进行排序,而后面的元素将被忽略。

>>> a_list = [['b', 'f'],['b', 'e'],['b', 'd'],['a', 'c'],['a', 'b'],['a', 'a'],]
>>> a_list.sort(lambda x,y : cmp(x[0], y[0]))
>>> a_list
[['a', 'c'], ['a', 'b'], ['a', 'a'], ['b', 'f'], ['b', 'e'], ['b', 'd']]

This is why the sort is described as a stable sort

这就是为什么排序被描述为稳定排序

>>> help(list.sort)
Help on method_descriptor:

sort(...)
    L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*;
    cmp(x, y) -> -1, 0, 1