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 = 0def __init__(self, i):self.id = idef __str__(self):return 'Data[%s]' % self.iddef 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参数指定自己的函数进行比较。

GitHub Repository.GitHub存储库中检出完整的python脚本和更多Python示例。

Reference: Official Documentation

参考: 官方文档

翻译自: https://www.journaldev.com/22980/python-max

python max

python max_Python max()相关推荐

  1. python exec_Python exec()

    python exec Python exec() function provides support for dynamic code execution. Python exec()函数提供了对动 ...

  2. Python中的“ @”(@)符号有什么作用?

    我正在看一些使用@符号的Python代码,但我不知道它的作用. 我也不知道要搜索什么,因为搜索Python文档时会出现,或者当包含@符号时Google不会返回相关结果. #1楼 此代码段: def d ...

  3. r语言min-max归一化_如何在R中使用min()和max()

    r语言min-max归一化 Finding min and max values is pretty much simple with the functions min() and max() in ...

  4. 如何使用PyTorch torch.max()

    In this article, we'll take a look at using the PyTorch torch.max() function. 在本文中,我们将介绍如何使用PyTorch ...

  5. 如何使用Python numpy.where()方法

    In Python, we can use the numpy.where() function to select elements from a numpy array, based on a c ...

  6. Python字符串title()

    Python String title() Python字符串title() Python String title() function returns a title cased version ...

  7. Python字符串isdecimal()

    Python String isdecimal() function returns True if all the characters in the string are decimal char ...

  8. Python字符串isalnum()

    Python string isalnum() function returns True if it's made of alphanumeric characters only. A charac ...

  9. Python字符串count()

    Python String count() function returns the number of occurrences of a substring in the given string. ...

最新文章

  1. sql实现两张表的拷贝
  2. echart多个柱状图 设置y轴显示_Origin神教程:柱状图还是2D的吗?也没有误差棒?...
  3. 04 | 深入浅出索引(上)
  4. iOS开发之share第三方登录以及分享
  5. C++11新特性之右值引用
  6. false true求或_如何依据【关键字】求【数量和】
  7. matlab smooth 函数,matlab中smooth函数平滑处理数据实例
  8. 关于time_wait状态的理解
  9. 拓端tecdat|R语言ggsurvplot绘制生存曲线报错 : object of type ‘symbol‘ is not subsettable
  10. Activiti6自学之路(一)—— Activiti6介绍
  11. KingbaseES 数据库本地化配置 LC_CTYPE 和 LC_COLLATE
  12. Clickhouse 函数基础入门
  13. 信用卡到底有什么好处?教你四个技巧拥有大额信用卡
  14. 欧美相关出口企业要注意了!
  15. 洞见 SELENIUM 自动化测试
  16. stm32f103电子钟心得体会_stm32f103时钟树讲解
  17. 谷歌地图启用全新卫星图:细节更清晰,色彩更丰富
  18. 【软考系统架构设计师】2009年下系统架构师案例分析历年真题
  19. 大数据分析培训好就业吗
  20. 【观察】AIoT赛道战鼓擂,生死破局谁才是真正的领跑者?

热门文章

  1. 第二篇:浅谈自定义头文件可能导致的重定义问题
  2. 使用annotation配置hibernate(1)
  3. NIOS II EDS NIOS II IDE 转载
  4. [转载] python divmod()函数
  5. [转载] Python数据分析:python与numpy效率对比
  6. Vimtutor中文版
  7. Python_Python处理JSON文件
  8. PHP 实例 AJAX 与 MySQL
  9. 为 Joomla 而生的 Kunena 论坛安装手册
  10. 《读编程珠玑有感》——细节处见技术