Python Max built-in functions detailed introduction to _python

Source: Internet
Author: User
Tags iterable

Python Max built-in functions

Max (iterable, *[, key, default))

Max (Arg1, arg2, *args[, key])

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

If One positional argument is provided, it should being 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 this 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 isn't provided, a valueerror is raised.

If multiple items are maximal, the function returns the "one" encountered. This is consistent with the other sort-stability preserving tools such as sorted (Iterable, Key=keyfunc, Reverse=true) [0] and H Eapq.nlargest (1, iterable, Key=keyfunc).

Description:

1. The function function is to take the maximum value from multiple parameters passed in, or to the maximum value in the Passed-in object element of the iteration. The default numeric type parameter, the value of the large person, the character parameter, the alphabetical table sorted 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 defaults that are returned when the maximum value does not exist.

2. The function passes in at least two arguments, but there is an exception that passes in only one parameter, at which point the argument must be an iterative object and return the largest element in the iterator object.

>>> Max (1) # incoming 1 parameter error
Traceback (most recent call last):
 File "<pyshell#0>", line 1, in <module& gt;
  Max (1)
typeerror: ' int ' object is not iterable
>>> max (1,2) # incoming 2 parameters 2 medium
2
>>> Max (1,2,3) # Pass 3 parameters to 3 of the larger
3
>>> max (' 1234 ') # Pass in an iterative object with its maximum element value
' 4 '

3. When the incoming parameter is inconsistent with the data type, all incoming parameters are then compared after implicit data type conversion, and an error is made if implicit data type conversions are not possible.

>>> Max (1,1.1,1.3e1) # Integer and floating-point number desirable maximum
13.0
>>> Max (1,2,3, ' 3 ') # Values and strings cannot be maximized
traceback ( Most recent called 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 desirable maximum
[1, 3]
>>> max ([1,2 ], (1,3)) # list and tuple cannot take maximum
Traceback (most recent call last):
 File "<pyshell#7>", line 1, in <module>
   max ([1,2], (1,3))
typeerror:unorderable types:tuple () > list ()

4. When multiple identical maximum values are present, the first occurrence of the maximum value is returned.

#定义a, B, c 3 List
>>> a = [1,2]
>>> b = [1,1]
>>> c = [1,2]

#查看a, B, c ID
>>> ID (a)
68128320
>>> ID (b)
68128680
>>> ID (c)
68128240

#取最大值
>>> d = max (a,b,c)
>>> ID (d)
68128320

#验证是否最大值是否是a
>> > id (a) = = ID (d)
True

5. The default numeric parameters, the value of large, character parameters, the alphabet sorted by the latter; the sequence parameter, then the maximum by the value of the index position. You can also specify the maximum value method by passing in the named parameter key.

>>> Max (1,2) # take the number
2
>>> max (' A ', ' B ') # to sort by the latter
' B '
>>> max (' ab ', ' ac ', ' Ad ') # by index comparison to the larger
' ad '

>>> max ( -1,0) # value defaults to the larger number
0
>>> max ( -1,0,key = ABS) # When the absolute value function is passed in, the parameter is evaluated and then the larger
-1

6. The other function of key parameters is that different types of objects can not compare to the maximum value, pass the appropriate key function, become able to compare the maximum value.

>>> Max (1,2, ' 3 ') #数值和字符串不能取最大值
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) # After specifying that the key is a conversion function, the maximum value of
' 3 '

>>> max ((1,2), [1,1]) #元组和列表不能取最大值
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]) #指定key为返回序列索引1位置的元素后, maximum value
(1, 2) can be taken 
    Copy Code

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

>>> Max (()) #空可迭代对象不能取最大值
Traceback (most recent):
 File "<pyshell#26>", line 1, in <m Odule>
  Max (())
Valueerror:max () arg is a empty sequence
>>> Max ((), default=0) #空可迭代对象, Specifies that the default parameter is
0
>>> Max ((), 0) #默认值必须使用命名参数进行传参, otherwise it will be considered 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, I hope to 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.