statlist = [('abc',5,1), ('bzs',66,1), ... ]
sorted(statlist, key=lambda x: int(x[1]))
I want to sort it by the integer largest to smallest. In this case, 5 and 66. But it doesn't seem to be working.
我想按整数从大到小排序。在这种情况下,5和66.但它似乎没有工作。
8 个解决方案
#1
7
The sorted
function returns a new list so you will need to assign the results of the function like this:
sorted函数返回一个新列表,因此您需要分配函数的结果,如下所示:
new_list = sorted(statlist, key=lambda x: int(x[1]))
#2
7
Use the .sort
method for in place sorting:
使用.sort方法进行就地排序:
statlist = [('abc',5,1), ('bzs',66,1), ... ]
statlist.sort(key=lambda x: int(x[1]))
If you do want to use sorted
, then reassign the variable:
如果您确实想使用sorted,则重新分配变量:
statlist = [('abc',5,1), ('bzs',66,1), ... ]
statlist = sorted(statlist, key=lambda x: int(x[1]))
For descending sort, use reverse
:
对于降序排序,请使用reverse:
statlist = [('abc',5,1), ('bzs',66,1), ... ]
statlist = sorted(statlist, key=lambda x: int(x[1]), reverse=True)
Then, you'd better use itemgetter
instead of a lambda
:
然后,你最好使用itemgetter而不是lambda:
import operator
statlist = [('abc',5,1), ('bzs',66,1), ... ]
statlist = sorted(statlist, key=operator.itemgetter(1), reverse=True)
#3
3
You can pass, key, and reverse to .sort function
您可以传递,键入和反向.sort函数
>>> x.sort(key=lambda x:x[1],reverse=True)
>>> x
[('bzs', 66, 1), ('abc', 5, 1)]
>>>
#4
3
for inplace sorting use
用于原地分类使用
statlist.sort(key=lambda x: x[1])
for creating other list, with sorted data use
用于创建其他列表,使用排序数据
otherlist = sorted( statlist, key=lambda x: x[1] )
#5
2
from operator import itemgetter
statlist = [('abc',5,1), ('bzs',66,1), ... ]
# statlist.sort modifiest the statlist, sorted returns a new one
# reverse puts the largest items to the front
statlist.sort(key=itemgetter(1), reverse=True)
#6
1
In response to alex's comment that he thought that sorted() worked "like the sort function":
为了回应alex的评论,他认为sorted()的工作方式“像sort函数一样”:
If it worked "like the sort function", it is unlikely to have been put in the library.
如果它像“排序函数”一样工作,则不太可能被放入库中。
In any case, there is no sort function ... you refer to the sort method of list objects.
在任何情况下,都没有排序功能......你可以参考列表对象的排序方法。
Simple demonstration using the interactive interpreter:
使用交互式解释器进行简单演示:
>>> alist = [3, 2, 1]; x = alist.sort(); print x; print alist
None
[1, 2, 3]
>>> alist = [3, 2, 1]; x = sorted(alist); print x; print alist
[1, 2, 3]
[3, 2, 1]
Here's a tip: look for patterns and similarities, but always verify your intuitive extrapolations. You might like to apply those ideas to reverse
and reversed
.
这是一个提示:寻找模式和相似之处,但始终验证您的直观推断。您可能希望将这些想法应用于反向和反转。
#7
0
>>> s = [('xyz', 8, 1), ('abc',5,1), ('bzs',66,1) ]
>>> s = sorted(s, key=lambda x: int(x[1]))
>>> s.reverse()
>>> print s
[('bzs', 66, 1), ('xyz', 8, 1), ('abc', 5, 1)]
#8
0
Hey when ever I am saving something to an array I don't tend to worry about order and then at the end I use sorted()
for example like this statlist = sorted(statlist)
and if you wanted it largest to smallest statlist = sorted(statlist, reverse = True)
That's the simple way of getting largest to smallest!
嘿,当我将某些东西保存到数组时,我不会担心顺序,然后最后我使用sorted()例如像statlist = sorted(statlist),如果你想要它最大到最小的statlist =已排序(statlist,reverse = True)这是从最小到最小的简单方法!
Example code where I've used this (just an extract)
我用过它的示例代码(只是一个提取)
while i <= math.sqrt(intnum):
if (intnum % i) == 0:
numbers.insert(0,i)
numbers.insert(0,int(intnum/i))
print(i,":", int(intnum/i))
i += 1
numbers = sorted(numbers, reverse = True)
#1
7
The sorted
function returns a new list so you will need to assign the results of the function like this:
sorted函数返回一个新列表,因此您需要分配函数的结果,如下所示:
new_list = sorted(statlist, key=lambda x: int(x[1]))
#2
7
Use the .sort
method for in place sorting:
使用.sort方法进行就地排序:
statlist = [('abc',5,1), ('bzs',66,1), ... ]
statlist.sort(key=lambda x: int(x[1]))
If you do want to use sorted
, then reassign the variable:
如果您确实想使用sorted,则重新分配变量:
statlist = [('abc',5,1), ('bzs',66,1), ... ]
statlist = sorted(statlist, key=lambda x: int(x[1]))
For descending sort, use reverse
:
对于降序排序,请使用reverse:
statlist = [('abc',5,1), ('bzs',66,1), ... ]
statlist = sorted(statlist, key=lambda x: int(x[1]), reverse=True)
Then, you'd better use itemgetter
instead of a lambda
:
然后,你最好使用itemgetter而不是lambda:
import operator
statlist = [('abc',5,1), ('bzs',66,1), ... ]
statlist = sorted(statlist, key=operator.itemgetter(1), reverse=True)
#3
3
You can pass, key, and reverse to .sort function
您可以传递,键入和反向.sort函数
>>> x.sort(key=lambda x:x[1],reverse=True)
>>> x
[('bzs', 66, 1), ('abc', 5, 1)]
>>>
#4
3
for inplace sorting use
用于原地分类使用
statlist.sort(key=lambda x: x[1])
for creating other list, with sorted data use
用于创建其他列表,使用排序数据
otherlist = sorted( statlist, key=lambda x: x[1] )
#5
2
from operator import itemgetter
statlist = [('abc',5,1), ('bzs',66,1), ... ]
# statlist.sort modifiest the statlist, sorted returns a new one
# reverse puts the largest items to the front
statlist.sort(key=itemgetter(1), reverse=True)
#6
1
In response to alex's comment that he thought that sorted() worked "like the sort function":
为了回应alex的评论,他认为sorted()的工作方式“像sort函数一样”:
If it worked "like the sort function", it is unlikely to have been put in the library.
如果它像“排序函数”一样工作,则不太可能被放入库中。
In any case, there is no sort function ... you refer to the sort method of list objects.
在任何情况下,都没有排序功能......你可以参考列表对象的排序方法。
Simple demonstration using the interactive interpreter:
使用交互式解释器进行简单演示:
>>> alist = [3, 2, 1]; x = alist.sort(); print x; print alist
None
[1, 2, 3]
>>> alist = [3, 2, 1]; x = sorted(alist); print x; print alist
[1, 2, 3]
[3, 2, 1]
Here's a tip: look for patterns and similarities, but always verify your intuitive extrapolations. You might like to apply those ideas to reverse
and reversed
.
这是一个提示:寻找模式和相似之处,但始终验证您的直观推断。您可能希望将这些想法应用于反向和反转。
#7
0
>>> s = [('xyz', 8, 1), ('abc',5,1), ('bzs',66,1) ]
>>> s = sorted(s, key=lambda x: int(x[1]))
>>> s.reverse()
>>> print s
[('bzs', 66, 1), ('xyz', 8, 1), ('abc', 5, 1)]
#8
0
Hey when ever I am saving something to an array I don't tend to worry about order and then at the end I use sorted()
for example like this statlist = sorted(statlist)
and if you wanted it largest to smallest statlist = sorted(statlist, reverse = True)
That's the simple way of getting largest to smallest!
嘿,当我将某些东西保存到数组时,我不会担心顺序,然后最后我使用sorted()例如像statlist = sorted(statlist),如果你想要它最大到最小的statlist =已排序(statlist,reverse = True)这是从最小到最小的简单方法!
Example code where I've used this (just an extract)
我用过它的示例代码(只是一个提取)
while i <= math.sqrt(intnum):
if (intnum % i) == 0:
numbers.insert(0,i)
numbers.insert(0,int(intnum/i))
print(i,":", int(intnum/i))
i += 1
numbers = sorted(numbers, reverse = True)