Python – max()函数
Python max()函数r返回可迭代项中的最大项或两个或多个参数中的最大项。
它有两种形式。
- 带有对象的 max()函数
- 可迭代的 max()函数
带有对象的 max()函数
与 C/C++ 的 max()函数不同, Python中的 max()函数可以接受任何类型的对象,并返回其中最大的。在字符串的情况下,它返回按字典顺序排列的最大值。
Syntax : max(arg1, arg2, *args[, key])
Parameters :
- arg1, arg2 : objects of the same datatype
- *args : multiple objects
- key : function where comparison of iterable is performed based on its return value
Returns : The maximum value
Python max()函数示例
示例 1:查找 3 个整数变量中的最大值
Python3
var1 = 4
var2 = 8
var3 = 2
max_val = max(var1, var2, var3)
print(max_val)
Python3
var1 = "geeks"
var2 = "for"
var3 = "geek"
max_val = max(var1, var2, var3)
print(max_val)
Python3
var1 = "geeks"
var2 = "for"
var3 = "geek"
max_val = max(var1, var2, var3,
key=len)
print(max_val)
Python3
integer = 5
string = "geek"
max_val = max(integer, string)
print(max_val)
Python3
list = [1.2, 1.3, 0.1]
max_value = max(list)
print(max_value)
Python3
# function to find minimum and maximum position in list
def minimum(a, n):
# inbuilt function to find the position of maximum
maxpos = a.index(max(a))
# printing the position
print ("The maximum is at position", maxpos + 1)
# driver code
a = [3, 4, 1, 3, 4, 5]
minimum(a, len(a))
Python3
string = "GeeksforGeeks"
max_val = max(string)
print(max_val)
Python3
string_list = ["Geeks", "for", "Geeks"]
max_val = max(string_list)
print(max_val)
Python3
string_list = ["Geeks", "for", "Geek"]
max_val = max(string_list, key=len)
print(max_val)
Python3
dictionary = {}
max_val = max(dictionary,
default={1: "Geek"})
print(max_val)
输出 :
8
示例 2:查找 3 个字符串变量的最大值
默认情况下,它将返回具有最大字典值的字符串。
Python3
var1 = "geeks"
var2 = "for"
var3 = "geek"
max_val = max(var1, var2, var3)
print(max_val)
输出 :
for
例3:根据长度求3个字符串变量的最大值
我们将在 max() 方法中传递一个关键函数。
Python3
var1 = "geeks"
var2 = "for"
var3 = "geek"
max_val = max(var1, var2, var3,
key=len)
print(max_val)
输出 :
geeks
示例 4: Python max() 异常
如果我们传递不同数据类型的参数,则会引发异常。
Python3
integer = 5
string = "geek"
max_val = max(integer, string)
print(max_val)
输出 :
TypeError: '>' not supported between instances of 'str' and 'int'
示例 5: Python max() 浮点数
Python3
list = [1.2, 1.3, 0.1]
max_value = max(list)
print(max_value)
输出:
1.3
示例 6: Python max() 索引
Python3
# function to find minimum and maximum position in list
def minimum(a, n):
# inbuilt function to find the position of maximum
maxpos = a.index(max(a))
# printing the position
print ("The maximum is at position", maxpos + 1)
# driver code
a = [3, 4, 1, 3, 4, 5]
minimum(a, len(a))
输出:
The maximum is at position 6
可迭代的 max()函数
当一个可迭代对象被传递给 max()函数时,它返回可迭代对象的最大项。
Syntax : max(iterable, *iterables[, key, default])
Parameters :
- iterable : iterable object like list or string.
- *iterables : multiple iterables
- key : function where comparison of iterable is performed based on its return value
- default : value if the iterable is empty
Returns : The maximum value.
示例 1:查找字符串中按字典顺序排列的最大字符
Python3
string = "GeeksforGeeks"
max_val = max(string)
print(max_val)
输出 :
s
示例 2:在字符串列表中查找字典顺序最大的字符串
Python3
string_list = ["Geeks", "for", "Geeks"]
max_val = max(string_list)
print(max_val)
输出 :
for
示例 3:在字符串列表中查找最长的字符串。
Python3
string_list = ["Geeks", "for", "Geek"]
max_val = max(string_list, key=len)
print(max_val)
输出 :
Geeks
例4:如果iterable为空,则显示默认值
Python3
dictionary = {}
max_val = max(dictionary,
default={1: "Geek"})
print(max_val)
输出 :
{1: 'Geek'}