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])
5 个解决方案
#1
27
Yes. The sorted
built-in accepts a key
argument:
是。已排序的内置接受一个关键参数:
sorted(li,key=lambda x: x[1])
Out[31]: [['Jason', 1], ['John', 2], ['Jim', 9]]
note that sorted
returns a new list. If you want to sort in-place, use the .sort
method of your list (which also, conveniently, accepts a key
argument).
请注意,sorted返回一个新列表。如果要在就地排序,请使用列表的.sort方法(也可以方便地接受键参数)。
or alternatively,
或者,
from operator import itemgetter
sorted(li,key=itemgetter(1))
Out[33]: [['Jason', 1], ['John', 2], ['Jim', 9]]
阅读python wiki上的更多内容。
#2
6
You can use the sorted method with a key.
您可以使用带键的排序方法。
sorted(a, key=lambda x : x[1])
#3
0
The optional key
parameter to sort
/sorted
is a function. The function is called for each item and the return values determine the ordering of the sort
排序/排序的可选键参数是一个函数。为每个项调用该函数,返回值确定排序的顺序
>>> 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 print
out of the function leaves
从功能中取出打印件
>>> def my_key_func(item):
... return item[1]
This function is simple enough to write "inline" as a lambda function
这个函数很简单,可以将“inline”写成lambda函数
>>> sorted(lst, key=lambda item: item[1])
[['Jason', 1], ['John', 2], ['Jim', 9]]
#4
0
You can use list.sort
with its optional key
parameter and a lambda
expression:
您可以将list.sort与其可选的键参数和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.itemgetter
instead of a lambda
:
请注意,对于大型列表,使用operator.itemgetter而不是lambda会更快:
>>> from operator import itemgetter
>>> lst = [
... ['John',2],
... ['Jim',9],
... ['Jason',1]
... ]
>>> lst.sort(key=itemgetter(1))
>>> lst
[['Jason', 1], ['John', 2], ['Jim', 9]]
>>>
#5
0
sorted(list, key=lambda x: x[1])
Note: this works on time variable too.
注意:这也适用于时间变量。
#1
27
Yes. The sorted
built-in accepts a key
argument:
是。已排序的内置接受一个关键参数:
sorted(li,key=lambda x: x[1])
Out[31]: [['Jason', 1], ['John', 2], ['Jim', 9]]
note that sorted
returns a new list. If you want to sort in-place, use the .sort
method of your list (which also, conveniently, accepts a key
argument).
请注意,sorted返回一个新列表。如果要在就地排序,请使用列表的.sort方法(也可以方便地接受键参数)。
or alternatively,
或者,
from operator import itemgetter
sorted(li,key=itemgetter(1))
Out[33]: [['Jason', 1], ['John', 2], ['Jim', 9]]
阅读python wiki上的更多内容。
#2
6
You can use the sorted method with a key.
您可以使用带键的排序方法。
sorted(a, key=lambda x : x[1])
#3
0
The optional key
parameter to sort
/sorted
is a function. The function is called for each item and the return values determine the ordering of the sort
排序/排序的可选键参数是一个函数。为每个项调用该函数,返回值确定排序的顺序
>>> 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 print
out of the function leaves
从功能中取出打印件
>>> def my_key_func(item):
... return item[1]
This function is simple enough to write "inline" as a lambda function
这个函数很简单,可以将“inline”写成lambda函数
>>> sorted(lst, key=lambda item: item[1])
[['Jason', 1], ['John', 2], ['Jim', 9]]
#4
0
You can use list.sort
with its optional key
parameter and a lambda
expression:
您可以将list.sort与其可选的键参数和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.itemgetter
instead of a lambda
:
请注意,对于大型列表,使用operator.itemgetter而不是lambda会更快:
>>> from operator import itemgetter
>>> lst = [
... ['John',2],
... ['Jim',9],
... ['Jason',1]
... ]
>>> lst.sort(key=itemgetter(1))
>>> lst
[['Jason', 1], ['John', 2], ['Jim', 9]]
>>>
#5
0
sorted(list, key=lambda x: x[1])
Note: this works on time variable too.
注意:这也适用于时间变量。