## As a python programmer, do you really know how to use the max() and min() functions?

Tianyuan prodigal son 2020-11-13 09:06:23
python programmer really know use

stay Python Of the more than 70 built-in functions of ,max() and min() They are two commonly used ones , Used to find the maximum and minimum values in a set of data . These two functions seem simple , In fact, there are many unexpected functions hidden , In especial key Parameters , Add similar to these two functions map() Function functions . If applied in code , There must be some magic work 、 Fantastic feeling , breathtaking ！

### 1. Typical application scenarios

This is a max() and min() The most common application scenario of function ： Returns the maximum from multiple numeric arguments / minimum value , Or return the largest in a numeric list / minimum value . The numerical type here , It refers to integers and floating points .

``````>>> max(1,3,5,7) # Returns the maximum value of multiple numeric parameters
7
>>> min([2,4,6,8]) # Returns the minimum value in a numeric list
2
``````

### 2. Compare strings

Type of input parameter or list element , Except for numerical , It can also be a string , But you can't mix numeric and string . The rule for comparing the size of two strings starts with the first character ASCII Code value , If equal , Then compare the last character , Until the size comes out .

``````>>> max(['a','b','c']) # Returns the largest string in the list
'c'
>>> min('2020-06-23 12:00:00', '2020-06-23 11:50:00') # Returns the earliest date time string
'2020-06-23 11:50:00'
>>> max('1', '9', '10', '100') # Returns the largest string among multiple strings
'9'
>>> min('1', '9', '10', '100') # Returns the smallest string among multiple strings
'1'
``````

### 3. Compare lists or tuples

Except for numerical 、 String type ,max() and min() Function can also be used to compare the size of a list or tuple ： Compare the elements in order , Until the size comes out . Of course , The elements used for comparison must be of the same type , It's either numerical , Either it's all string type .

``````>>> max([8,4],[6,8]) # Returns the largest list of multiple lists
[8, 4]
>>> max([8,4],[8,4,2]) # Returns the largest list of multiple lists
[8, 4, 2]
>>> min(['8',4],['8',4,'2']) # Returns the smallest list of multiple lists
['8', 4]
>>> min(('8',4, '3'),('8',4,'2')) # Returns the smallest tuple among multiple tuples
('8', 4, '2')
``````

### 4. Find the dictionary 、 character string 、 The maximum and minimum in an iterator

max() and min() Function can not only compare the size of elements in a list or tuple , You can also gather from 、 Dictionaries 、 character string 、range object 、 Find the largest and smallest elements in objects such as iterators .

``````>>> max({
3,4,5,6}) # Returns the maximum value in the collection
6
>>> min({
'x':5, 'y':7, 'z':3}) # Returns the minimum value in a dictionary element , The result is the smallest bond
'x'
>>> max("I'm xufive.") # Returns the maximum character in a string （ It's a bit of an accident ？）
'x'
>>> min("I'm xufive.") # Returns the smallest character in a string
' '
>>> max(range(5)) # range() The function returns a range object
4
>>> max(map(abs, [-1,2,-3])) # map() Function returns an iterator
3
``````

### 5. Use key Parameters

We noticed that , Use max() and min() Function returns the maximum of the dictionary / minimum value , What you actually get is the largest key in the dictionary / minimum value . So how to get the maximum value of the dictionary / What about the minimum value ？ It's simple , Use max() and min() Functional key Parameters . however , One caveat , Use key Parameters , The largest in the dictionary is returned / The minimum key , Not the biggest / minimum value .

``````>>> d = {
'x':5, 'y':7, 'z':3}
>>>> min(d) # Returns the smallest key in the dictionary
'x'
>>> min(d, key=lambda x:d[x]) # The key that returns the minimum value in the dictionary , Not the minimum
'z'
>>> max(d) # Returns the largest key in the dictionary
'z'
>>> max(d, key=lambda x:d[x]) # The key that returns the maximum value in the dictionary , Not the maximum
'y'
``````

### 6. Comprehensive practice

Last , Let's do two exercises , have a look max() and min() How powerful a function is . These two questions are not difficult , Beginners can give answers easily , however , If you want to write elegantly and beautifully , Not max() and min() Functions cannot be .

1. list [1,3,5,7,9,11,13,15,17] in , Which element has the smallest sine value ？
2. list [8,5,2,4,3,6,5,5,1,4,5] in , Which element appears most frequently ？

problem 1 The reference answer to ：

``````>>> import math
>>> a = [1,3,5,7,9,11,13,15,17]
>>> min(a, key=math.sin)
11
>>> math.sin(11) # Elements 11 The sine value of is the smallest
-0.9999902065507035
``````

problem 2 The reference answer to ：

``````>>> a = [8,5,2,4,3,6,5,5,1,4,5]
>>> max(set(a), key=a.count)
5
>>> a.count(5) # Elements 5 There is 4 Time , Most times
4
``````