简体   繁体   English

在Python中以数字方式对列表进行排序

[英]Sort a list numerically in Python

So I have this list, we'll call it listA. 所以我有这个列表,我们称之为listA。 I'm trying to get the [3] item in each list eg ['5.01','5.88','2.10','9.45','17.58','2.76'] in sorted order. 我试图按照排序顺序得到每个列表中的[3]项,例如['5.01','5.88','2.10','9.45','17.58','2.76'] So the end result would start the entire list over again with Santa at the top. 因此,最终结果将在圣诞老人的顶部重新开始整个列表。 Does that make any sense? 这有任何意义吗?

[['John Doe', u'25.78', u'20.77', '5.01'], ['Jane Doe', u'21.08', u'15.20', '5.88'], ['James Bond', u'20.57', u'18.47', '2.10'], ['Michael Jordan', u'28.50', u'19.05', '9.45'], ['Santa', u'31.13', u'13.55', '17.58'], ['Easter Bunny', u'17.20', u'14.44', '2.76']]

If you want to return the complete list in sorted order, this may work. 如果要按排序顺序返回完整列表,这可能有效。 This takes your input list and runs sorted on top of it. 这将获取您的输入列表并在其上运行sorted The reverse argument set to True sorts the list in reverse (descending) order, and the key argument specifies the method by which to sort, which in this case is the float of the third argument of each list: 设置为Truereverse参数按反向(降序)顺序对列表进行排序, key参数指定排序的方法,在本例中是每个列表的第三个参数的float

In [5]: l = [['John Doe', u'25.78', u'20.77', '5.01'] # continues...    
In [6]: sorted(l, reverse=True, key=lambda x: float(x[3]))
Out[6]:
[['Santa', u'31.13', u'13.55', '17.58'],
 ['Michael Jordan', u'28.50', u'19.05', '9.45'],
 ['Jane Doe', u'21.08', u'15.20', '5.88'],
 ['John Doe', u'25.78', u'20.77', '5.01'],
 ['Easter Bunny', u'17.20', u'14.44', '2.76'],
 ['James Bond', u'20.57', u'18.47', '2.10']]

If you only need the values in sorted order, the other answers provide viable ways of doing so. 如果您只需要排序顺序的值,其他答案提供了可行的方法。

Try this: 尝试这个:

>>> listA=[['John Doe', u'25.78', u'20.77', '5.01'], ['Jane Doe', u'21.08', u'15.20', '5.88'], ['James Bond', u'20.57', u'18.47', '2.10'], ['Michael Jordan', u'28.50', u'19.05', '9.45'], ['Santa', u'31.13', u'13.55', '17.58'], ['Easter Bunny', u'17.20', u'14.44', '2.76']]

>>> [x[3] for x in sorted(listA, reverse = True, key = lambda i : float(i[3]))]
['17.58', '9.45', '5.88', '5.01', '2.76', '2.10']

An anonymous lambda function is not necessary for this task. 此任务不需要匿名lambda函数。 You can use operator.itemgetter , which may be more intuitive: 您可以使用operator.itemgetter ,它可能更直观:

from operator import itemgetter

res1 = sorted(map(itemgetter(3), listA), reverse=True, key=float)

['17.58', '9.45', '5.88', '5.01', '2.76', '2.10']
from operator import itemgetter
lstA.sort(reverse = True, key = itemgetter(3))

And you are good to go. 你很高兴。 Using itemgetter() within a sort is extremely helpful when you have multiple indexs to sort on. 当您有多个要排序的索引时,在排序中使用itemgetter()非常有用。 Lets say you wanted to sort alphabetically on the first value in case of a tie you could do the following 假设您希望按照字母顺序对第一个值进行排序,如果出现平局,您可以执行以下操作

from operator import itemgetter
lstA.sort(key = itemgetter(0))
lstA.sort(reverse = True, key = itemgetter(3))

And Voilà! 和Voilà!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM