In the python3.x world, the CMP function is gone. So what's the use of a function that Sorted,min,max to compare functions as arguments?
Take the definition of the Min function as an example, there are two overloaded forms:
Single parameter (one iterator):
Copy the Code code as follows:
Min (iterable[, Key=func]), value
Multi-parameter (multiple content to compare):
Copy the Code code as follows:
Min (A, B, C, ...) [, Key=func]) -Value
This paper mainly discusses the use of key=func parameters. Let's illustrate:
1. Comparison of custom objects
I have defined a class test with two member variables A and B:
Copy the Code code as follows:
Class Test:
def __init__ (self,a,b):
SELF.A = A
SELF.B = b
It then instantiates three objects, X, Y, Z:
Copy CodeThe code is as follows:
X=test (' x ')
Y=test (2, ' Y ')
Z=test (8, ' Z ')
I want them to compare the values of variable A to the smallest object of a:
Copy CodeThe code is as follows:
Mintest=min (X,y,z,key=lambda t:t.a)
Because the key parameter needs to pass in a function, it is convenient to use the Lambda anonymous function. In this example, to implement the comparison function (exactly the comparison key function), so LAMDA parameters as long as one, whatever you take what name (I use T), representing the object to be compared (i.e., a,b,c); the colon is followed by an expression, which returns the member variable a of t directly.
Thus, the Min function (which is similar to a function such as max,sorted) will be compared according to the a value of each object to be compared, returning the object with the smallest value of a (reference) assigned to Mintest.
Output mintest.a,mintest.b to verify the results.
2. Comparison of dictionary values value
There is a dictionary:
Copy the Code code as follows:
dic={' B ': 3, ' a ': 5, ' C ': 9, ' d ': 2}
If you want to sort by key keys for the dictionary, simply:
Copy CodeThe code is as follows:
Sorted (DIC)
Returns a list, which is the sorted key, but the value is not placed in the list:
Copy CodeThe code is as follows:
[' A ', ' B ', ' C ', ' d ']
The following methods are good:
Copy CodeThe code is as follows:
>>> Sorted (Dic.items ())
[(' A ', 5), (' B ', 3), (' C ', 9), (' d ', 2)]
What if you want to sort by value? Then it is good to pass the parameter of the comparison function:
Copy the Code code as follows:
Sorted (Dic.items (), Key=lambda d:d[1])
I continue to use the Lambda anonymous function. where d represents each iteration element in Dic.items (), which is a tuple (for example (' a ', 5)), the expression d[1] is the second element in the tuple (for example, 5), and it is the value of the dictionary, and we need to compare it to the standard. Operation Result:
Copy CodeThe code is as follows:
[(' d ', 2), (' B ', 3), (' A ', 5), (' C ', 9)]
P.S.
Bloggers today, the Python challenge encountered a level, need to count the number of characters appearing in the text and find the fewest characters. Of course, that one does not need to write their own comparison function, output statistical results can be seen by the naked eye. Bo Master with the help of the search engine min function key=func parameters, deep pain Chinese python Introduction article is the Old World python2.x, full of the new world can not use the grammar, to python3.x beginners bring a lot of misleading, harmful useless. So out of this article.
Bloggers are also beginners of python, it is a great honor for Daniel to criticize and correct him.