- 2020-07-29 06:35
*views 6*- python On Tao
- max
- min
- Python

stay Python Of the more than 70 built-in functions of ,max() and min() Are two of the more commonly used , Used to find the maximum and minimum values in a set of data . These two functions look simple , In fact, there are many unexpected functions hidden , in especial key parameter , A similar function is added to these two functions map() Function of function . If applied in code , There must be some magic work , Fantastic feeling , It's amazing !

<>1. Typical application scenarios

This is max() and min() The most common application scenarios of function ： Returns the maximum from multiple numeric arguments / minimum value , Or return the maximum in a numeric list / minimum value . The numerical type here , Refers to integers and floating-point types .

>>> 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 the numerical type , It can also be a string , But it 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 , The next character is compared , Until the size is separated .

>>> 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 of multiple strings '9' >>> min('1', '9', '10', '100') #

Returns the smallest of multiple strings '1'

<>3. Compare list or tuple

Except for the numerical type , String type ,max() and min() Function can also be used to compare the size of a list or tuple ： Compare the elements in sequence , Until the size is separated . of course , The elements used for comparison must be of the same type , Either it's all numerical , Either they're all strings .

>>> 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 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 , Maximum and minimum in iterator

max() and min() Function can not only compare the size of elements in a list or tuple , It can also be assembled 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 key 'x' >>> max("I'm xufive.") # Returns the maximum character in a string （ A little bit unexpected ?） 'x' >>> min

("I'm xufive.") # Returns the smallest character in a string ' ' >>> max(range(5)) # range() Function returns a range object 4 >>>

max(map(abs, [-1,2,-3])) # map() Function returns an iterator 3

<>5. use key parameter

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 parameter . however , What needs to be reminded is , use key parameter , Returns the largest in the dictionary / Key for minimum value , 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]) # Returns the key for the maximum value in the dictionary , Not the maximum 'y'

<>6. Comprehensive exercises

last , Let's do two exercises , have a look max() and min() How powerful are functions . These two questions are not difficult , Beginners can easily give answers , however , To write elegantly and beautifully , wrong max() and min() Function not allowed .

* list [1,3,5,7,9,11,13,15,17] in , Which element has the smallest sine value ?

* list [8,5,2,4,3,6,5,5,1,4,5] in , Which element appears most frequently ?

problem 1 Reference answer for ：

>>> import math >>> a = [1,3,5,7,9,11,13,15,17] >>> min(a, key=math.sin) 11 >>>

math.sin(11) # element 11 The sine value of is the smallest -0.9999902065507035

problem 2 Reference answer for ：

>>> a = [8,5,2,4,3,6,5,5,1,4,5] >>> max(set(a), key=a.count) 5 >>> a.count(5)

# element 5 There it is 4 second , Most times 4

Technology

Daily Recommendation

views 26

views 2

©2019-2020 Toolsou All rights reserved,

Image format conversion Count the number of letters （java Language implementation ） Using orthogonal table method to design test cases One is called “ Asking for the train ” A small village Finally got the train VaR - Value at risk - Monte Carlo method - Python How to build data security system ?python: Convert a file to a binary file (binary)vue use vue-clipboard2 Realize the function of copy link 13. solve git Merge conflict uniapp Summary of page value transfer