程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Explain the first parameter key of the python list method sort() [simple and clear explanation]

編輯:Python

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


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved