英文文档:

max(iterable, *[, key, default])

max(arg1, arg2, *args[, key])

Return the largest item in an iterable or the largest of two or more arguments.

If one positional argument is provided, it should be an iterable. The largest item in the iterable is returned. If two or more positional arguments are provided, the largest of the positional arguments is returned.

There are two optional keyword-only arguments. The key argument specifies a one-argument ordering function like that used for list.sort(). The default argument specifies an object to return if the provided iterable is empty. If the iterable is empty and default is not provided, a ValueError is raised.

If multiple items are maximal, the function returns the first one encountered. This is consistent with other sort-stability preserving tools such as sorted(iterable, key=keyfunc, reverse=True)[0] and heapq.nlargest(1, iterable, key=keyfunc).

说明:

  1. 函数功能为取传入的多个参数中的最大值,或者传入的可迭代对象元素中的最大值。默认数值型参数,取值大者;字符型参数,取字母表排序靠后者。还可以传入命名参数key,其为一个函数,用来指定取最大值的方法。default命名参数用来指定最大值不存在时返回的默认值。

  2. 函数至少传入两个参数,但是有只传入一个参数的例外,此时参数必须为可迭代对象,返回的是可迭代对象中的最大元素。

>>> max(1) # 传入1个参数报错
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
max(1)
TypeError: 'int' object is not iterable
>>> max(1,2) # 传入2个参数 取2个中较大者
2
>>> max(1,2,3) # 传入3个参数 取3个中较大者
3
>>> max('') # 传入1个可迭代对象,取其最大元素值
''

  3. 当传入参数为数据类型不一致时,传入的所有参数将进行隐式数据类型转换后再比较,如果不能进行隐式数据类型转换,则会报错。

>>> max(1,1.1,1.3E1) # 整数与浮点数可取最大值
13.0
>>> max(1,2,3,'') # 数值与字符串不能取最大值
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
max(1,2,3,'')
TypeError: unorderable types: str() > int() >>> max([1,2],[1,3]) # 列表与列表可取最大值
[1, 3]
>>> max([1,2],(1,3)) # 列表与元组不能取最大值
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
max([1,2],(1,3))
TypeError: unorderable types: tuple() > list()

  4. 当存在多个相同的最大值时,返回的是最先出现的那个最大值。

#定义a、b、c 3个列表
>>> a = [1,2]
>>> b = [1,1]
>>> c = [1,2] #查看a、b、c 的id
>>> id(a)
68128320
>>> id(b)
68128680
>>> id(c)
68128240 #取最大值
>>> d = max(a,b,c)
>>> id(d)
68128320 #验证是否最大值是否是a
>>> id(a) == id(d)
True

  5. 默认数值型参数,取值大者;字符型参数,取字母表排序靠后者;序列型参数,则依次按索引位置的值进行比较取最大者。还可以通过传入命名参数key,指定取最大值方法。

>>> max(1,2) # 取数值大者
2
>>> max('a','b') # 取排序靠后者
'b'
>>> max('ab','ac','ad') # 依次按索引比较取较大者
'ad' >>> max(-1,0) # 数值默认去数值较大者
0
>>> max(-1,0,key = abs) # 传入了求绝对值函数,则参数都会进行求绝对值后再取较大者
-1

  6. key参数的另外一个作用是,不同类型对象本来不能比较取最大值的,传入适当的key函数,变得可以比较能取最大值了。

>>> max(1,2,'') #数值和字符串不能取最大值
Traceback (most recent call last):
File "<pyshell#21>", line 1, in <module>
max(1,2,'')
TypeError: unorderable types: str() > int()
>>> max(1,2,'',key = int) # 指定key为转换函数后,可以取最大值
'' >>> max((1,2),[1,1]) #元组和列表不能取最大值
Traceback (most recent call last):
File "<pyshell#24>", line 1, in <module>
max((1,2),[1,1])
TypeError: unorderable types: list() > tuple()
>>> max((1,2),[1,1],key = lambda x : x[1]) #指定key为返回序列索引1位置的元素后,可以取最大值
(1, 2)

  7. 当只传入的一个可迭代对象时,而且可迭代对象为空,则必须指定命名参数default,用来指定最大值不存在时,函数返回的默认值。

>>> max(()) #空可迭代对象不能取最大值
Traceback (most recent call last):
File "<pyshell#26>", line 1, in <module>
max(())
ValueError: max() arg is an empty sequence
>>> max((),default=0) #空可迭代对象,指定default参数为默认值
0
>>> max((),0) #默认值必须使用命名参数进行传参,否则将被认为是一个比较的元素
Traceback (most recent call last):
File "<pyshell#27>", line 1, in <module>
max((),0)
TypeError: unorderable types: int() > tuple()

Python内置函数(41)——max的更多相关文章

  1. Python内置函数(3)——max

    英文文档: max(iterable, *[, key, default]) max(arg1, arg2, *args[, key]) Return the largest item in an i ...

  2. Python内置函数(41)——id

    英文文档: id(object) Return the "identity" of an object. This is an integer which is guarantee ...

  3. Python之路(第八篇)Python内置函数、zip()、max()、min()

    一.python内置函数 abs() 求绝对值 例子 print(abs(-2)) all() 把序列中每一个元素做布尔运算,如果全部都是true,就返回true, 但是如果是空字符串.空列表也返回t ...

  4. Python之路Python内置函数、zip()、max()、min()

    Python之路Python内置函数.zip().max().min() 一.python内置函数 abs() 求绝对值 例子 print(abs(-2)) all() 把序列中每一个元素做布尔运算, ...

  5. Python内置函数和内置常量

    Python内置函数 1.abs(x) 返回一个数的绝对值.实参可以是整数或浮点数.如果实参是一个复数,返回它的模. 2.all(iterable) 如果 iterable 的所有元素为真(或迭代器为 ...

  6. Python | 内置函数(BIF)

    Python内置函数 | V3.9.1 | 共计155个 还没学完, 还没记录完, 不知道自己能不能坚持记录下去 1.ArithmeticError 2.AssertionError 3.Attrib ...

  7. python 内置函数和函数装饰器

    python内置函数 1.数学相关 abs(x) 取x绝对值 divmode(x,y) 取x除以y的商和余数,常用做分页,返回商和余数组成一个元组 pow(x,y[,z]) 取x的y次方 ,等同于x ...

  8. 那些年,很多人没看懂的Python内置函数

    Python之所以特别的简单就是因为有很多的内置函数是在你的程序"运行之前"就已经帮你运行好了,所以,可以用这个的特性简化很多的步骤.这也是让Python语言变得特别的简单的原因之 ...

  9. Python 内置函数笔记

    其中有几个方法没怎么用过, 所以没整理到 Python内置函数 abs(a) 返回a的绝对值.该参数可以是整数或浮点数.如果参数是一个复数,则返回其大小 all(a) 如果元组.列表里面的所有元素都非 ...

随机推荐

  1. 利用easygui模块编写的华氏温度与摄氏温度转换的小程序

    -*- coding:utf-8 -*- #Author:'Lmc' #DATE: 2019/4/23/0023 下午 4:23:08 #FileName:tem_compare_gui.PY imp ...

  2. Eclipse 那些小技巧(值得收藏)

    1.菜单命令系列 Edit→content Assist→add Alt+/ 代码关联 Windows→Next Editor→add Ctrl+Tab 切换窗口 Run→Debug Toggle L ...

  3. String.length()和String.getBytes().length

    1.字符与字节 抛出如下代码: public static void main(String[] args) { String str = "活出自己范儿"; System.out ...

  4. MFC学习RepositionBars

    学习过程中用到了就找了下资料 https://blog.csdn.net/u012372584/article/details/50718968

  5. Linux-信号量与P,V操作

    Linux-信号量与P,V操作 内容 使用信号量实现进程互斥 使用信号量及PV实现子进程读写同步 机理 Linux信号量集 Linux信号量作为IPC机制的一种,与其他通信方式类似,Linux也是通过 ...

  6. 爬虫之requests模块

    requests模块 什么是requests模块 requests模块是python中原生的基于网络请求的模块,其主要作用是用来模拟浏览器发起请求.功能强大,用法简洁高效.在爬虫领域中占据着半壁江山的 ...

  7. Java对象转换成Map

    需求总是千奇百怪,对象转成map,看似没必要,但就是有这个需求,所以记录下来 首先是Bean package tools; import lombok.Data; /** * 车辆实体类 */ @Da ...

  8. springboot添加邮件发送及压缩功能

    springboot添加邮件发送及文件压缩功能 转载请注明出处:https://www.cnblogs.com/funnyzpc/p/9190233.html 先来一段诗 ``` 就这样吧 忍受折磨 ...

  9. pip install –r ./requirements.txt 报错 改成 pip install -r requirements.txt 成功

    Invalid requirement: '–r'Traceback (most recent call last): File "/home/dev/.pyenv/versions/3.6 ...

  10. 平时作业五 Java

    使用I/O流和文件对象实现目录备份功能.用户指定源目录.目标目录以及备份文件类型(如果是任意文件使用通配符*号),通过此程序可将源目录及其所有子目录下的指定类型文件保存到目标目录. package c ...