在本教程中,我们将讨论 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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。