Python list (list) Methods sort() The grammar is as follows :
list.sort( key=None, reverse=False)
Both parameters are optional , The second parameter goes without saying , It's simple , Is to control whether the order is ascending or descending .
This article focuses on the first parameter key The role of .
So parameters key What is the function of ?
in general , We can use it to sort by the criteria we want , That is, we can customize the sorting criteria , Or the custom sorting method .
Parameters key Is the return value of a function , This function has only one input parameter , That is each element of the list we want to sort ( This function is executed as many times as there are elements in the list to be sorted ), The return value of this function is used as the basis for sorting the list elements .
The above sentence is not easy to understand , Headless tie , Just look at a few examples .
First look at the nested list ( 2 d list ) The default sort of .
list1 = [[1, 7], [1, 5], [2, 4], [1, 1]]
list1.sort()
The operation results are as follows :
From the running results, we can see that , The default sorting method is to first sort by the number of each list element 0 Elements to sort , Then press the... Of each list element 1 Elements to sort .
If we want to sort by the number of each list element 1 Elements to sort , What shall I do? ?
Just write as follows .
def sort_fun(x1):
return x1[1]
list1 = [[1, 7], [1, 5], [2, 4], [1, 1]]
list1.sort(key=sort_fun)
The operation results are as follows :
We see , The above result is based on the number of each list element 1 Elements to sort .
How to achieve it ?
sort() Method after execution , Will list1 Four list elements in [1, 7]、[1, 5]、 [2, 4]、[1, 1] As a function in turn sort_fun Input parameters of , And get four return values in turn , The four return values have respective indexes of 1 The element value of , namely 7、5、4、1 These four values , The four values are sorted in ascending order and then 1、4、5、7, So four list elements [1, 7]、[1, 5]、 [2, 4]、[1, 1] Is sorted as [1, 1]、[2, 4]、[1, 5]、 [1, 7].
We can use anonymous functions lambda To simplify the above .
About anonymous functions lambda See the following article for an introduction to :
https://zhuanlan.zhihu.com/p/58579207
Using anonymous functions lambda To simplify the code above :
list1 = [[1, 7], [1, 5], [2, 4], [1, 1]]
list1.sort(key=lambda x1: x1[1])
The running results are the same as those above :
Another example , We want to sort the strings in the list by length , So we could write it like this :
list1 = ['baidu', 'CSDN', 'QQ', 'Google', 'suwenhao']
list1.sort(key=lambda x1: len(x1))
The operation results are as follows :
How to achieve it ?
sort() Method after execution , The five string elements ’baidu’, ‘CSDN’, ‘QQ’, ‘Google’, 'suwenhao’ They are respectively substituted into anonymous functions as input parameters , And their lengths are obtained in turn 5、4、2、6、8, In ascending order of length, it is 2、4、5、6、8, So the original sequence is sorted to :[‘QQ’, ‘CSDN’, ‘baidu’, ‘Google’, ‘suwenhao’]
As we can see from the above example , Method sort() The first parameter of key The function called has only one input parameter , That is, each element of the list to be sorted , If we want to have more parameters for stronger sorting , What shall I do? ? You can refer to another blog post of mine https://blog.csdn.net/wenhao_ir/article/details/125407158