英文文檔:
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)
.
說明:
1. 函數功能為取傳入的多個參數中的最大值,或者傳入的可迭代對象元素中的最大值。默認數值型參數,取值大者;字符型參數,取字母表排序靠后者。還可以傳入命名參數key,其為一個函數,用來指定取最大值的方法。default命名參數用來指定最大值不存在時返回的默認值。
2. 函數至少傳入兩個參數,但是有只傳入一個參數的例外,此時參數必須為可迭代對象,返回的是可迭代對象中的最大元素。
可迭代對象,字符串,類表,元組,字典、集合都可以比較,
>>> max(1,3,4) 4 >>> max('1234') '4' >>> max((1,2,3,4)) 4 >>> max((1,2,3,6,8)) 8 >>> max([1,2,3,4]) 4 >>> max({'x':1,'y':2}) 'y' >>> max({'x':1,'y':2,'z':10}) 'z'
3.當傳入參數數據類型不一致時,傳入的參數會進行隱式數據類型轉換,如果不能進行隱式轉換就會報錯,
>>> max(1,2.4545) # 整數和浮點數可以,有多個值時,先比較前兩個再跟第三個比較,以此類推, 2.4545 >>> max(1,'a') # 整數和字符串不可以 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: '>' not supported between instances of 'str' and 'int' >>> >>> max([1,3],[2,3]) # 列表和列表可以,它們是按位比較的, [2, 3] >>> max([1,3],[1,4]) [1, 4] >>> max([1,3,4,5,6],[1,4,2,5,7,1]) [1, 4, 2, 5, 7, 1] >>> max([1,3,4,5,6],[1,4,2,]) [1, 4, 2] >>> max([1,3,4,5,6],[2]) [2] >>> >>> max([1,2,3],(1,3)) # 列表和元組不可以 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: '>' not supported between instances of 'tuple' and 'list' >>> SyntaxError: invalid syntax >>> max({1,3,4,5}) 5 >>> >>> max({1,3},(1,2)) # 集合和列表也不可以。 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: '>' not supported between instances of 'tuple' and 'set'
4. 當存在多個相同的最大值時,返回的是最先出現的那個最大值。
#定義a、b、c 3個列表 >>> 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.比較的方式:
默認數值類型:取值大者
字符串類型:按字母表排序靠后者
序列類型:依次按索引位置取值比較取最大者。
還可以傳入命名參數 key,指定取最大值的方法.
>>> max(-1,-5,key = abs) # 先執行 abs 函數在執行 max 函數 -5 >>> key 另一個作用是:原本數據類型不一致,不能進行比較的,傳入適當的 key 就可以比較。通過傳入的函數,將兩者的數據類型轉換為一致 >>> max(1,3,'5')Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: '>' not supported between instances of 'str' and 'int' >>> max(1.3,'5',key=int) '5' >>> max(1,3,'5') Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: '>' not supported between instances of 'str' and 'int' >>> max(1.3,'5',key=int) # 整數和字符串 '5' >>> >>> max([1,2],(1,3),key=lambda x:x[1]) #列表和元組 (1, 3) >>>
6.空的可迭代對象不能進行比較,必須傳入一個默認值。
>>> max([1,2],(1,3),key=lambda x:x[1]) (1, 3) >>> >>> >>> max(()) Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: max() arg is an empty sequence >>> max((),default=1) 1 >>> max((),0) #默認值必須用命名參數進行傳參,否會被認為是一個比較的元素。 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: '>' not supported between instances of 'int' and 'tuple'