Python max built-in function details, pythonmax
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 shocould 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 ).
Note:
1. The function is used to take the maximum value of multiple input parameters or the maximum value of the input iteratable object element. The default value is Numeric. The value is greater than the default value. The value is sorted by the alphabetic order by the latter. You can also pass in the named parameter key as a function to specify the method to take the maximum value. The default naming parameter is used to specify the default value returned when the maximum value does not exist.
2. The function must input at least two parameters, but only one parameter is required. In this case, the parameter must be an Iterated object and the maximum element in the iterated object is returned.
>>> Max (1) # input 1 parameter error Traceback (most recent call last): File "<pyshell #0>", line 1, in <module> max (1) TypeError: 'int' object is not iterable >>> max (1, 2) # input 2 parameters to take 2 of the larger 2> max (1234, 3) # input 3 parameters to take 3 of the larger 3> max ('123 ') # input one iteratable object and obtain its maximum element value '4'
3. When the input parameters are of different data types, all input parameters are converted to implicit data types before comparison. If implicit data type conversion is not supported, an error is returned.
>>> Max (1, 1.1, 1.3E1) # The integer and floating point number can reach the maximum value of 13.0 >>> max (, 3, '3 ') # The maximum value Traceback (most recent call last) cannot be used for values and strings: File "<pyshell #5>", line 1, in <module> max (, 3, '3') TypeError: unorderable types: str ()> int () >>> max ([1, 2], [1, 3]) # list and list max values can be obtained [1, 3] >>> max ([1, 2], (1, 3) # The maximum Traceback (most recent call last) cannot be obtained for the list and tuples: File "<pyshell #7> ", line 1, in <module> max ([1, 2], (1, 3) TypeError: unorderable types: tuple ()> list ()
4. If there are multiple identical maximum values, the return value is the first maximum value.
# Define the list of a, B, and c> a = [1, 2]> B = [1, 1]> c = [1, 2] # view a, B, c id >>> id () 68128320 >>>> id (B) 68128680 >>> id (c) 68128240 # obtain the maximum >>> d = max (a, B, c) >>> id (d) 68128320 # verify whether the maximum value is a >>> id (a) = id (d) True
5. Default Value: Numeric parameter. The value is greater than the upper parameter. The upper parameter is sorted by the alphabet and the lower parameter is compared by index value. You can also specify the maximum value method by passing in the name parameter key.
>>> Max (1, 2) # greater than 2 >>> max ('A', 'B ') # sort by 'B'> max ('AB', 'ac', 'ad ') # 'ad'> max (-) # by default, the value is greater than 0> max (-, key = abs) # If the absolute value function is input, the parameter will evaluate the absolute value and then take the greater-1
6. Another function of the key parameter is that different types of objects cannot compare the maximum values. If you input an appropriate key function, the maximum value can be obtained.
>>> Max (, '3') # The maximum value Traceback (most recent call last) cannot be used for values and strings: File "<pyshell #21>", line 1, in <module> max (, '3') TypeError: unorderable types: str ()> int () >>> max (, '3', key = int) # After the key is specified as the conversion function, you can take the maximum value '3'> max (), []) # maximum Traceback (most recent call last) cannot be used for tuples and lists: File "<pyshell #24>", line 1, in <module> max (1, 2 ), []) TypeError: unorderable types: list ()> tuple ()> max (), [], key = lambda x: x [1]) # If you specify the key as the element at the position 1 of the returned sequence index, you can copy the code at the maximum value (1, 2 ).
7. When only one iteratable object is passed in and the iteratable object is empty, you must specify the name parameter default to specify the default value returned by the function when the maximum value does not exist.
>>> Max () # The maximum Traceback (most recent call last) cannot be obtained for an empty iteratable object: File "<pyshell #26>", line 1, in <module> max () ValueError: max () arg is an empty sequence >>> max (), default = 0) # empty iteratable object, specify the default parameter as the default value: 0> max (), 0) # The default value must use the name parameter for parameter passing, otherwise it will be considered as a comparison element Traceback (most recent call last): File "<pyshell #27>", line 1, in <module> max (), 0) typeError: unorderable types: int ()> tuple ()
Thank you for reading this article. I hope it will help you. Thank you for your support for this site!