Python 如何按列对多维数组进行排序?

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

How to sort multidimensional array by column?

pythonarrayslistsorting

提问by Web Hopeful

Is there a way to use the sort() method or any other method to sort a list by column? Lets say I have the list:

有没有办法使用 sort() 方法或任何其他方法按列对列表进行排序?假设我有清单:

[
[John,2],
[Jim,9],
[Jason,1]
]

And I wanted to sort it so that it would look like this:

我想对其进行排序,使其看起来像这样:

[
[Jason,1],
[John,2],
[Jim,9],
]

What would be the best approach to do this?

这样做的最佳方法是什么?

Edit:

编辑:

Right now I am running into an index out of range error. I have a 2 dimensional array that is lets say 1000 rows b 3 columns. I want to sort it based on the third column. Is this the right code for that?

现在我遇到了索引超出范围的错误。我有一个二维数组,可以说 1000 行 b 3 列。我想根据第三列对其进行排序。这是正确的代码吗?

sorted_list = sorted(list_not_sorted, key=lambda x:x[2])

采纳答案by roippi

Yes. The sortedbuilt-in accepts a keyargument:

是的。该sorted内置接受key的说法:

sorted(li,key=lambda x: x[1])
Out[31]: [['Jason', 1], ['John', 2], ['Jim', 9]]

note that sortedreturns a new list. If you want to sort in-place, use the .sortmethod of your list (which also, conveniently, accepts a keyargument).

请注意,sorted返回一个新列表。如果您想就地排序,请使用.sort列表的方法(它也方便地接受一个key参数)。

or alternatively,

或者,

from operator import itemgetter
sorted(li,key=itemgetter(1))
Out[33]: [['Jason', 1], ['John', 2], ['Jim', 9]]

Read more on the python wiki.

在 python wiki 上阅读更多内容

回答by squiguy

You can use the sorted method with a key.

您可以使用带有键的 sorted 方法。

sorted(a, key=lambda x : x[1])

回答by roippi

You can use list.sortwith its optional keyparameterand a lambdaexpression:

您可以使用list.sort其可选key参数lambda表达式

>>> lst = [
...     ['John',2],
...     ['Jim',9],
...     ['Jason',1]
... ]
>>> lst.sort(key=lambda x:x[1])
>>> lst
[['Jason', 1], ['John', 2], ['Jim', 9]]
>>>

This will sort the list in-place.

这将就地对列表进行排序。



Note that for large lists, it will be faster to use operator.itemgetterinstead of a lambda:

请注意,对于大型列表,使用operator.itemgetter代替 a会更快lambda

>>> from operator import itemgetter
>>> lst = [
...     ['John',2],
...     ['Jim',9],
...     ['Jason',1]
... ]
>>> lst.sort(key=itemgetter(1))
>>> lst
[['Jason', 1], ['John', 2], ['Jim', 9]]
>>>

回答by John La Rooy

The optional keyparameter to sort/sortedis a function. The function is called for each item and the return values determine the ordering of the sort

/的可选key参数是一个函数。为每个项目调用该函数,返回值确定排序的顺序sortsorted

>>> lst = [['John', 2], ['Jim', 9], ['Jason', 1]]
>>> def my_key_func(item):
...     print("The key for {} is {}".format(item, item[1]))
...     return item[1]
... 
>>> sorted(lst, key=my_key_func)
The key for ['John', 2] is 2
The key for ['Jim', 9] is 9
The key for ['Jason', 1] is 1
[['Jason', 1], ['John', 2], ['Jim', 9]]

taking the printout of the function leaves

print出来的功能叶

>>> def my_key_func(item):
...     return item[1]

This function is simple enough to write "inline" as a lambda function

这个函数很简单,可以将“内联”写成一个 lambda 函数

>>> sorted(lst, key=lambda item: item[1])
[['Jason', 1], ['John', 2], ['Jim', 9]]

回答by kevin

sorted(list, key=lambda x: x[1])

Note: this works on time variable too.

注意:这也适用于时间变量。