在本教程中,我們將討論 max() 函數以及如何在 Python 編程語言中使用它。我們還將考慮各種示例以更好地理解。
所以,讓我們開始吧。
了解 Python max() 函數
Python 中的 max() 函數返回可迭代對象中最重要的數據元素。我們還可以使用此函數來查找多個參數之間的最大數據元素。
Python max() 函數有兩種不同的形式:
- max()使用 iterable 作為參數的函數
- max()使用對象作為參數的函數
帶有可迭代參數的 max() 函數
我們可以使用以下語法來查找可迭代對象中最大的數據元素:
用法:
max(iterable, *iterables, key, default)
參數:
- iterable:這個參數是可迭代的。例如,元組、列表、字典、集合等等。
- *iterables (optional):這是一個可選參數,指示多個可迭代對象。
- key (optional):這也是一個可選參數。此參數在傳遞可迭代對象時起作用,並根據其返回值進行比較。
- default (optional):這是另一個可選參數,如果指定的可迭代對象為 void,則為函數提供默認值。
讓我們考慮一個基於此方法的示例,以查找列表中的最大元素。
範例:1
# creating a list
my_digits = [12, 3, 8, 4, 15, 13, 10, 2, 9, 11]
# using the max() function
large = max(my_digits)
# printing the result
print("The largest number in the list:", large)
輸出:
The largest number in the list:15
說明:
在上麵的代碼片段中,我們創建了一個列表作為 my_digits。然後我們在列表上使用了 max() 函數以獲得列表中的最大元素。最後,我們為用戶打印了結果元素。
如果可迭代對象中的數據元素是字符串,則 max() 函數將返回最大的元素(按字母順序排列)。
讓我們考慮一個基於列表中最大字符串的示例。
示例:2
# creating a list
my_strings = ["Apple", "Mango", "Banana", "Papaya", "Orange"]
# using the max() function
large_str = max(my_strings)
# printing the result
print("The largest string in the list:", large_str)
輸出:
The largest string in the list:Papaya
說明:
在上麵的代碼片段中,我們將一個列表定義為由各種字符串組成的 my_strings。然後我們在列表上使用 max() 函數來查找作為字符串的最大元素。最後,我們為用戶打印了元素。
通常,max() 函數在字典的情況下返回最大的鍵。我們還可以使用 key 參數來查找字典中具有最大值的鍵。
讓我們考慮一個示例,演示如何在字典中使用 max() 函數。
示例:3
# creating a list
my_dict = {1:1, -3:9, 2:4, -5:25, 4:16}
# using the max() function to find largest key
key_1 = max(my_dict)
# printing the resultant key
print("The largest key in the dictionary:", key_1)
# using the key() function to find the key with largest value
key_2 = max(my_dict, key = lambda x:my_dict[x])
# printing the resultant key
print("The Key of the largest value in the dictionary:", key_2)
# printing the largest value
print("The largest value in the dictionary:", my_dict[key_2])
輸出:
The largest key in the dictionary:4 The Key of the largest value in the dictionary:-5 The largest value in the dictionary:25
說明:
在上麵的代碼片段中,我們創建了一個字典,其中一些值尊重它們的鍵。然後我們使用 max() 函數來查找字典中的最大鍵。然後我們使用 max() 函數找到具有最大值的鍵並為用戶打印該值。
而且,我們可以觀察到,在上麵例子中max()函數的第二條語句中,我們傳遞了一個lambda函數給key參數。
key = lambda x:my_dict[x]
此函數將返回字典的值。根據返回值,max() 函數將返回具有最大值的鍵。
要記住的要點:
如果將 void 迭代器傳遞給函數,則會引發 ValueError 異常。為了避免這種異常,我們可以在函數中包含默認參數。
在多個迭代器的情況下,將返回指定迭代器中最大的元素。
帶有對象參數的 max() 函數
我們可以使用以下語法來找到多個參數之間的最大對象。
用法:
max(arg1, arg2, *args, key)
參數:
- arg1:這個參數是一個對象。例如,數字、字符串等等。
- arg2:這個參數也是一個對象。例如,數字、字符串等等。
- *args (optional):此參數是更多對象的可選參數。
- key (optional):該參數也是一個可選參數,在每個參數傳遞時起作用,並根據其返回值進行比較。
因此,max() 函數幫助我們找到多個對象之間的最大元素。讓我們考慮一個例子,以便在給定的數字中找到最大的數字。
例:
# using the max() function with objects as numbers
large_num = max(10, -4, 5, -3, 13)
# printing the result
print("The largest number:", large_num)
輸出:
The largest number:13
說明:
在上麵的代碼片段中,我們使用 max() 函數在指定對象中查找最大元素作為參數,並將結果打印給用戶。
相關用法
- Python string max()用法及代碼示例
- Python max() and min()用法及代碼示例
- Python Tkinter maxsize()用法及代碼示例
- Python matplotlib.patches.Rectangle用法及代碼示例
- Python matplotlib.pyplot.step()用法及代碼示例
- Python math.cos()用法及代碼示例
- Python math.cosh()用法及代碼示例
- Python math.acosh()用法及代碼示例
- Python matplotlib.axes.Axes.pie()用法及代碼示例
- Python map()用法及代碼示例
- Python math.fmod()用法及代碼示例
- Python math.fsum()用法及代碼示例
- Python matplotlib.pyplot.semilogy()用法及代碼示例
- Python math.remainder()用法及代碼示例
- Python math.asinh()用法及代碼示例
- Python matplotlib.pyplot.inferno()用法及代碼示例
- Python matplotlib.axes.Axes.semilogx()用法及代碼示例
- Python math.atanh()用法及代碼示例
- Python math.fabs()用法及代碼示例
- Python math.log1p()用法及代碼示例
注:本文由純淨天空篩選整理自 max() function in Python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。