python max
Python max() function returns the largest item in the iterable or the largest of two or more arguments.
Python max()函数返回可迭代的最大项或两个或多个参数的最大项。
Python max() (Python max())
Python max() function syntax is:
Python max()函数语法为:
max(iterable, *[, key, default])
max(arg1, arg2, *args[, key])
- If there is only one argument, it should be an iterable such as string, list, tuple etc. The largest item in the iterable is returned. 如果只有一个参数,则它应该是可迭代的,例如string , list , tuple等。返回可迭代的最大项。
- If two or more arguments are provided, largest of them will be returned. 如果提供了两个或多个参数,则将返回最大的参数。
- We can specify
key
argument function to be used for identifying the largest item. This is an optional argument and mostly used when arguments are custom objects. 我们可以指定用于确定最大项目的key
变量函数。 这是一个可选参数,通常在参数是自定义对象时使用。 - The
default
argument specifies an object to return if the provided iterable is empty. If the iterable is empty and default is not provided,ValueError
exception is raised.default
参数指定如果提供的iterable为空,则返回的对象。 如果iterable为空且未提供默认值,则会引发ValueError
异常。 - If multiple largest element is found, then the first one is returned. 如果找到多个最大元素,则返回第一个元素。
Python max()函数示例 (Python max() function examples)
Let’s look at some examples of max() function.
让我们看一下max()函数的一些示例。
带字符串的max() (max() with string)
When max() function is used with string argument, the character with maximum unicode value is returned.
当max()函数与字符串参数一起使用时,将返回具有最大unicode值的字符。
s = 'abcCba'
print(max(s))
print('c' > 'C')
Output:
输出:
c
True
带元组的max() (max() with tuple)
tuple_numbers = (1, 2, 3, 4)
print(max(tuple_numbers))
Output: 4
输出4
列表的最大值 (max of list)
list_numbers = [1, 2, 3, -4]
print(max(list_numbers))
Output: 3
输出3
对象的max() (max() of objects)
When we want to use max() function with custom objects, we have to provide key
function argument to be used for comparing the objects.
当我们想对自定义对象使用max()函数时,我们必须提供用于比较对象的key
函数参数。
class Data:
id = 0
def __init__(self, i):
self.id = i
def __str__(self):
return 'Data[%s]' % self.id
def get_data_id(data):
return data.id
# max() with objects and key argument
list_objects = [Data(1), Data(2), Data(-10)]
print(max(list_objects, key=get_data_id))
Output: Data[2]
输出: Data[2]
If we don’t provide a key function as an argument, we will get the following error.
如果我们不提供键函数作为参数,则会出现以下错误。
TypeError: '>' not supported between instances of 'Data' and 'Data'
max()具有可迭代的空值和默认值 (max() with empty iterable and default value)
print(max([], default=20))
Output: 20
输出: 20
具有多个参数的max()函数 (max() function with multiple arguments)
print(max(1, 2, 3, 4))
Output: 4
输出4
带有参数和键函数的max() (max() with arguments and key function)
def str_length(s):
return len(s)
print(max('a', 'abc', 'ab', key=str_length))
Output: abc
输出: abc
具有多个可迭代项的max() (max() with multiple iterables)
x1 = [10, 20, 30]
x2 = [5, 15, 40, 25]
print(max(x1, x2, key=len))
Output: [5, 15, 40, 25]
输出: [5, 15, 40, 25]
If we don’t provide key function as an argument, the output will be [10, 20, 30]
. It’s because the comparison will be done between the elements of the iterable elements one by one. When an element with the larger value is found, the iterable with that element will be returned.
如果我们不提供key函数作为参数,则输出为[10, 20, 30]
。 这是因为将在可迭代元素的元素之间进行比较。 当找到具有较大值的元素时,将返回该元素的可迭代对象。
max()具有多个对象的可迭代 (max() with multiple iterables of objects)
x1 = [Data(10), Data(20), Data(30)]
x2 = [Data(5), Data(15), Data(40), Data(25)]
max_list = max(x1, x2, key=len)
for x in max_list:
print(x)
Output:
输出:
Data[5]
Data[15]
Data[40]
Data[25]
Notice that with multiple arguments, iterables are treated as objects. If we don’t specify key function, we will get error message as TypeError: '>' not supported between instances of 'Data' and 'Data'
. Earlier it worked with integer elements because they support > and < operators.
注意,使用多个参数,可迭代对象被视为对象。 如果不指定键功能,则会收到错误消息,类型错误TypeError: '>' not supported between instances of 'Data' and 'Data'
。 之前它可以使用整数元素,因为它们支持>和<运算符。
摘要 (Summary)
Python max() function helps us in identifying the largest element in the iterable or largest item from multiple arguments. It’s useful because we can specify our own function to be used for comparison through key argument.
Python max()函数可帮助我们从多个参数中确定可迭代项或最大项中的最大元素。 这非常有用,因为我们可以通过key参数指定自己的函数进行比较。
Reference: Official Documentation
参考: 官方文档
python max