Use python's sorted function, which sorts from small to large by default
sorted(list) returns an object that can be used as an expression. The original list is unchanged, and a new sorted list object is generated.
>>> a =[5,2,9,8,6]>>> a =sorted(a)>>>print(a)[2,5,6,8,9]
Reverse sorting is sorting from big to small, use reverse=True
>>> a =[5,2,9,8,6]>>> a =sorted(a,reverse=True)>>>print(a)[9,8,6,5,2]
When the list element is a tuple element, we need to use the parameter keyword key, lambda is an implicit function, it is a fixed way of writing, x represents an element in the list, here, it means a tuple, x is any name; x[ 0] means the first element in the tuple, and the second element is x[1].
Sort by first keyword
>>> a =[('x',0.56),('a',1.28),('c',2.36),('s',5.02),('h',20)]>>> a =sorted(a,key = lambda x:x[0])>>>print(a)[('a',1.28),('c',2.36),('h',20),('s',5.02),('x',0.56)]
Sort by the second keyword
>>> a =[('x',0.56),('a',1.28),('c',2.36),('s',5.02),('h',20)]>>> a =sorted(a,key = lambda x:x[1])>>>print(a)[('x',0.56),('a',1.28),('c',2.36),('s',5.02),('h',20)]
Reverse sorting is sorting from big to small, use reverse=True
>>> a =[('x',0.56),('a',1.28),('c',2.36),('s',5.02),('h',20)]>>> a =sorted(a,key = lambda x:x[1],reverse=True)>>>print(a)[('h',20),('s',5.02),('c',2.36),('a',1.28),('x',0.56)]
Use the reverse function to reverse the order of the elements in the list
>>> a =[('x',0.56),('a',1.28),('c',2.36),('s',5.02),('h',20)]>>> a.reverse()>>>print(a)[('h',20),('s',5.02),('c',2.36),('a',1.28),('x',0.56)]
list.sort() will not return an object, changing the original list.
list.sort(func=None, key=None, reverse=False)
Forward sort
>>> L =[2,3,1,4]>>>L.sort()>>>L
>>>[1,2,3,4]>>> L =[2,3,1,4]>>> a=L.sort()>>> a #Where a is None
>>> L
[1,2,3,4]
Reverse sort
>>> L =[2,3,1,4]>>>L.sort(reverse=True)>>>L
>>>[4,3,2,1]
Sort the second keyword
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - method 1------------------------------------>>>L =[('b',6),('a',1),('c',3),('d',4)]>>>L.sort(lambda x,y:cmp(x[1],y[1]))>>>L
>>>[(' a',1),('c',3),('d',4),('b',6)]-----------------------------------------Method 2------------------------------------>>>L =[('b',6),('a',1),('c',3),('d',4)]>>>L.sort(key=lambda x:x[1])>>>L
>>>[(' a',1),('c',3),('d',4),('b',6)]-----------------------------------------Method 3------------------------------------>>>L =[('b',2),('a',1),('c',3),('d',4)]>>>import operator
>>> L.sort(key=operator.itemgetter(1))>>>L
>>>[(' a',1),('b',2),('c',3),('d',4)]------------------------------------(DSU method:Decorate-Sort-Undercorate)------------------------------------->>>L =[('b',2),('a',1),('c',3),('d',4)]>>>A =[(x[1],i,x)for i,x inenumerate(L)] #i can confirm the stable sort
>>> A.sort()>>>L =[s[2]for s in A]>>>L
>>>[(' a',1),('b',2),('c',3),('d',4)]
What if we want to sort by the second keyword and then sort by the first keyword? There are two ways
>>> L =[('d',2),('a',4),('b',3),('c',2)]>>> L.sort(key=lambda x:(x[1],x[0]))>>> L
>>>[(' c',2),('d',2),('b',3),('a',4)]----------------------------------------------------------------------->>> L =[('d',2),('a',4),('b',3),('c',2)]>>> L.sort(key=operator.itemgetter(1,0))>>> L
>>>[(' c',2),('d',2),('b',3),('a',4)]
Reference: https://www.cnblogs.com/qilin20/p/12301878.html
https://www.cnblogs.com/python960410445/p/11831393.html
Recommended Posts