Pythonmax built-in functions

Source: Internet
Author: User
Tags iterable
This article mainly introduces more information about PythonMAX built-in functions. For more information, see 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) # one input parameter returns the error Traceback (most recent call last): File"
 
  
", Line 1, in
  
   
Max (1) TypeError: 'int' object is not iterable >>> max (1, 2) # Input 2 parameters take 2 of the larger 2> max (1, 2, 3) # pass in 3 parameters, take 3 of the larger ones, 3> max ('000000') # pass in 1 iteratable object, take 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 of the value and string cannot be Traceback (most recent call last): File"
 
  
", Line 1, in
  
   
Max (, 3, '3') TypeError: unorderable types: str ()> int () >>> max ([], []) # maximum values of the list and list can be [1, 3] >>> max ([1, 2], (1, 3) # maximum Traceback (most recent call last) cannot be obtained for the list and tuples ): file"
   
    
", Line 1, in
    
     
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"
 
  
", Line 1, in
  
   
Max (1, 2, '3') TypeError: unorderable types: str ()> int () >>> max (1, 2, '3', key = int) # After the key is specified as the conversion function, you can take the maximum value '3'> max (), []) # The maximum value Traceback (most recent call last) cannot be used for tuples and lists: File"
   
    
", Line 1, in
    
     
Max (), []) TypeError: unorderable types: list ()> tuple () >>> max (), [], key = lambda x: x [1]) # After specifying the key as the element at the position 1 of the returned sequence index, you can obtain 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"
 
  
", Line 1, in
  
   
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"
   
    
", Line 1, in
    
     
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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.