The Python max built in functions are described in detail

  • 2020-05-17 05:43:18
  • OfStack

Python max built-in functions

max(iterable, *[, key, default])

max(arg1, arg2, *args[, key])

Return the largest item in an iterable or the largest of two or more arguments.

If one positional argument is provided, it should be an iterable. The largest item in the iterable is returned. If two or more positional arguments are provided, the largest of the positional arguments is returned.

There are two optional keyword-only arguments. The key argument specifies a one-argument ordering function like that used for list.sort(). The default argument specifies an object to return if the provided iterable is empty. If the iterable is empty and default is not provided, a ValueError is raised.

If multiple items are maximal, the function returns the first one encountered. This is consistent with other sort-stability preserving tools such as sorted(iterable, key=keyfunc, reverse=True)[0] and heapq.nlargest(1, iterable, key=keyfunc).

Description:

1. The function function is to take the maximum value of multiple parameters passed in, or the maximum value of iterable object elements passed in. Default numeric parameter, the value is large; Character parameter, take the alphabetical order by the latter. You can also pass in the named parameter key, which is a function that specifies the method to take the maximum value. The default named parameter is used to specify the default value to return if the maximum does not exist.

2. If the function passes in at least two parameters, but only one parameter is passed in, the parameter must be an iterable object and the largest element in the iterable object is returned.


>>> max(1) #  The incoming 1 Error reporting for three parameters 
Traceback (most recent call last):
 File "<pyshell#0>", line 1, in <module>
  max(1)
TypeError: 'int' object is not iterable
>>> max(1,2) #  The incoming 2 A parameter   take 2 The larger of the two 
2
>>> max(1,2,3) #  The incoming 3 A parameter   take 3 The larger of the two 
3
>>> max('1234') #  The incoming 1 An iterable object, taking its maximum element value 
'4'

3. When the parameters passed in are of different data types, all parameters passed in will be compared after implicit data type conversion. If the implicit data type conversion cannot be carried out, an error will be reported.


>>> max(1,1.1,1.3E1) #  Integers and floating point Numbers can be maximized 
13.0
>>> max(1,2,3,'3') #  Values and strings cannot be maximized 
Traceback (most recent call last):
 File "<pyshell#5>", line 1, in <module>
  max(1,2,3,'3')
TypeError: unorderable types: str() > int()

>>> max([1,2],[1,3]) #  List and list can take maximum value 
[1, 3]
>>> max([1,2],(1,3)) #  Lists and tuples cannot be maximized 
Traceback (most recent call last):
 File "<pyshell#7>", line 1, in <module>
  max([1,2],(1,3))
TypeError: unorderable types: tuple() > list()

4. When there are multiple identical maxima, the one that appears first is returned.


# define a , b , c 3 A list of 
>>> a = [1,2]
>>> b = [1,1]
>>> c = [1,2]

# To view a , b , c  the id
>>> id(a)
68128320
>>> id(b)
68128680
>>> id(c)
68128240

# Taking the maximum 
>>> d = max(a,b,c)
>>> id(d)
68128320

# Verify that the maximum is a
>>> id(a) == id(d)
True

5. Default numeric parameters, with high values; Character parameter, take the alphabetical order by the latter; For a sequential parameter, compare the largest by the value of the index position in turn. You can also specify the Max method by passing in the named parameter key.


>>> max(1,2) #  The one with the highest value 
2
>>> max('a','b') #  Pick the sort by the latter 
'b'
>>> max('ab','ac','ad') #  Compare the larger by index in turn 
'ad'

>>> max(-1,0) #  The value defaults to the larger value 
0
>>> max(-1,0,key = abs) #  If the absolute value function is passed in, the parameter will be evaluated and then the larger one will be taken 
-1

6. Another effect of the key parameter is that objects of different types should not be able to compare the maximum value, but the appropriate key function is passed in, making it possible to compare the maximum value.


>>> max(1,2,'3') # Values and strings cannot be maximized 
Traceback (most recent call last):
 File "<pyshell#21>", line 1, in <module>
  max(1,2,'3')
TypeError: unorderable types: str() > int() 
>>> max(1,2,'3',key = int) #  The specified key Is the conversion function, you can take the maximum value 
'3'

>>> max((1,2),[1,1]) # Tuples and lists cannot be maximized 
Traceback (most recent call last):
 File "<pyshell#24>", line 1, in <module>
  max((1,2),[1,1])
TypeError: unorderable types: list() > tuple()
>>> max((1,2),[1,1],key = lambda x : x[1]) # The specified key Is the index of the returned sequence 1 After the position of the element, you can take the maximum value 
(1, 2)
 Copy the code 
  

7. When only one iterable object is passed in and the iterable object is empty, the named parameter default must be specified to specify the default value that the function returns when the maximum value does not exist.


>>> max(()) # Empty iterable objects cannot be maximized 
Traceback (most recent call last):
 File "<pyshell#26>", line 1, in <module>
  max(())
ValueError: max() arg is an empty sequence
>>> max((),default=0) # Empty iterable object, specified default Parameter is the default value 
0
>>> max((),0) # The default value must be passed with a named parameter, otherwise it will be assumed to be 1 The elements of comparison 
Traceback (most recent call last):
 File "<pyshell#27>", line 1, in <module>
  max((),0)
TypeError: unorderable types: int() > tuple()

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: