本文共 4230 字,大约阅读时间需要 14 分钟。
python max
Python max() function returns the largest item in the or the largest of two or more arguments.
Python max()函数返回的最大项或两个或多个参数的最大项。
Python max() function syntax is:
Python max()函数语法为:
max(iterable, *[, key, default])max(arg1, arg2, *args[, key])
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
变量函数。 这是一个可选参数,通常在参数是自定义对象时使用。 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
异常。 Let’s look at some examples of max() function.
让我们看一下max()函数的一些示例。
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:
输出:
cTrue
tuple_numbers = (1, 2, 3, 4)print(max(tuple_numbers))
Output: 4
输出4
list_numbers = [1, 2, 3, -4]print(max(list_numbers))
Output: 3
输出3
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.iddef get_data_id(data): return data.id# max() with objects and key argumentlist_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'
print(max([], default=20))
Output: 20
输出: 20
print(max(1, 2, 3, 4))
Output: 4
输出4
def str_length(s): return len(s)print(max('a', 'abc', 'ab', key=str_length))
Output: abc
输出: abc
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]
。 这是因为将在可迭代元素的元素之间进行比较。 当找到具有较大值的元素时,将返回该元素的可迭代对象。
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'
。 之前它可以使用整数元素,因为它们支持>和<运算符。
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:
参考:
翻译自:
python max
转载地址:http://rtmzd.baihongyu.com/