"Go" Python max built-in functions detailed description

Source: Internet
Author: User

#max ()    array1 = Range (Ten)  array2 = Range (0, 3) print (  ' max (array1) = ', Max (array1))  print (' Max ( array2) = ', Max (array2)) Print  (' Max (array1,) = ', Max (array1, Key=lambda x:x > 3)) Print  (max (1, 2))  Print (Max (' ah ', ' BF ', Key=lambda x:x[1]))  Print (max (array1, Array2, Key=lambda x:x[1])    def comparator (x):     return x[2]  

Description:

1. The function function is to take the maximum value in multiple parameters passed in, or the maximum value in an iterative object element passed in. The default numeric type parameter, the value is large, the character parameter, the alphabetical table is 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 value returned when the maximum value does not exist.

2. The function passes at least two parameters, but there is an exception for passing in only one parameter, at which point the argument must be an iterative object, returning the largest element in the object that can iterate.

>>> Max (1) # incoming 1 parameters Error Traceback (most recent call last): File "<pyshell#0>", line 1, in <module>  m Ax (1) TypeError: ' int ' object is not iterable>>> Max # incoming 2 parameters take 2 of the larger 2>>> Max (three-in-one) # Pass in 3 parameters take 3 medium larger 3>>> Max (' 1234 ') # passes in an iterative object, taking its maximum element value ' 4 '

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

>>> Max (1,1.1,1.3e1) # Integer with floating-point number 13.0>>> max (3 ') # numeric value and string cannot take maximum traceback (most recent call Last): File "<pyshell#5>", line 1, in <module>  max (3 ') typeerror:unorderable types:str () > int  () >>> Max ([1,2],[1,3]) # list and list desirable maximum value [1, 3]>>> Max ([1,3]) # list and tuple cannot take maximum traceback (most recent call Last): File ' <pyshell#7> ', line 1, in <module>  Max ([up], (1,3)) typeerror:unorderable types:tuple () ; List ()

  

4. When there are multiple identical maximums, the maximum value that appears first is returned.

#定义a, B, c 3 lists >>> a = [1,2]>>> b = [1,1]>>> c = [all] #查看a, B, c id>>> ID (a) 68128320> >> ID (b) 68128680>>> ID (c) 68128240 #取最大值 >>> d = max (A,B,C) >>> ID (d) 68128320 # Verify whether the maximum value is a>>> ID (a) = = ID (d) True

5. The default numeric parameter, the value is large, the character parameter, the alphabetical table is sorted by the latter; sequence parameter, then the index position by the value of the largest person to compare. You can also specify the maximum value method by passing in the named parameter key.

>>> Max # takes the value of 2>>> max (' A ', ' B ') # to sort by the latter ' B ' >>> max (' ab ', ' AC ', ' ad ') # by index comparison to the larger by the ' Ad ' & Gt;>> Max ( -1,0) # value defaults to the larger value 0>>> max ( -1,0,key = ABS) # passed in the absolute value function, then the parameter will be the absolute value and then take the larger person-1

6. Another function of the key parameter is that the different types of objects can not be compared to the maximum value, the appropriate key function, to be able to compare the maximum value.

>>> Max (3 ') #数值和字符串不能取最大值Traceback (most recent call last): File "<pyshell#21>", line 1, in <module >  max (3 ') typeerror:unorderable types:str () > int () >>> max (3 ', key = int) # After specifying key as the conversion function,  You can take the maximum value of ' 3 ' >>> max ([recent]) #元组和列表不能取最大值Traceback (most of the last): File "<pyshell#24>", line 1, In <module>  Max (typeerror:unorderable) types:list () > tuple () >>> max ([+]), Key = Lambda x:x[1]) #指定key为返回序列索引1位置的元素后, the maximum value (1, 2) can be taken to copy the code

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

>>> Max (()) #空可迭代对象不能取最大值Traceback (most recent): File "<pyshell#26>", line 1, in <module>< C0/>max (()) Valueerror:max () arg is an empty sequence>>> max ((), default=0) #空可迭代对象, specifying the default parameter as the defaults 0>> > Max ((), 0) #默认值必须使用命名参数进行传参, otherwise it would be considered a comparison element Traceback (most recent called last): File "<pyshell#27>", line 1, in < ;module>  Max ((), 0) typeerror:unorderable types:int () > tuple ()

Original address: http://www.jb51.net/article/97571.htm

"Go" Python max built-in functions detailed description

Related Article

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.