英文文档:

max(iterable*[, keydefault])

max(arg1arg2*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,3,4)
4
>>> max('1234')
'4'
>>> max((1,2,3,4))
4
>>> max((1,2,3,6,8))
8
>>> max([1,2,3,4])
4
>>> max({'x':1,'y':2})
'y'
>>> max({'x':1,'y':2,'z':10})
'z'

  3.当传入参数数据类型不一致时,传入的参数会进行隐式数据类型转换,如果不能进行隐式转换就会报错,

   >>> max(1,2.4545)  # 整数和浮点数可以,有多个值时,先比较前两个再跟第三个比较,以此类推,
2.4545
>>> max(1,'a') # 整数和字符串不可以
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: '>' not supported between instances of 'str' and 'int'
>>>
>>> max([1,3],[2,3]) # 列表和列表可以,它们是按位比较的,
[2, 3]
>>> max([1,3],[1,4])
[1, 4]
>>> max([1,3,4,5,6],[1,4,2,5,7,1])
[1, 4, 2, 5, 7, 1]
>>> max([1,3,4,5,6],[1,4,2,])
[1, 4, 2]
>>> max([1,3,4,5,6],[2])
[2]
>>>
>>> max([1,2,3],(1,3)) # 列表和元组不可以
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: '>' not supported between instances of 'tuple' and 'list'
>>>
SyntaxError: invalid syntax
>>> max({1,3,4,5})
5
>>>
>>> max({1,3},(1,2)) # 集合和列表也不可以。
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: '>' not supported between instances of 'tuple' and 'set'

  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,-5,key = abs)  # 先执行 abs 函数在执行 max 函数
-5
>>> key 另一个作用是:原本数据类型不一致,不能进行比较的,传入适当的 key 就可以比较。通过传入的函数,将两者的数据类型转换为一致 >>> max(1,3,'5')Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: '>' not supported between instances of 'str' and 'int'
>>> max(1.3,'5',key=int)
'5' >>> max(1,3,'5')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: '>' not supported between instances of 'str' and 'int'
>>> max(1.3,'5',key=int) # 整数和字符串
'5'
>>>
>>> max([1,2],(1,3),key=lambda x:x[1]) #列表和元组
(1, 3)
>>>

  

 6.空的可迭代对象不能进行比较,必须传入一个默认值。

>>> max([1,2],(1,3),key=lambda x:x[1])
(1, 3)
>>>
>>>
>>> max(())
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: max() arg is an empty sequence
>>> max((),default=1)
1
>>> max((),0) #默认值必须用命名参数进行传参,否会被认为是一个比较的元素。
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: '>' not supported between instances of 'int' and 'tuple'

  

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

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

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

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

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

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

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

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

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

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

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

  6. Python 内置函数笔记

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

  7. 【转】python 内置函数总结(大部分)

    [转]python 内置函数总结(大部分) python 内置函数大讲堂 python全栈开发,内置函数 1. 内置函数 python的内置函数截止到python版本3.6.2,现在python一共为 ...

  8. python内置函数,匿名函数

    一.匿名函数 匿名函数:为了解决那些功能很简单的需求而设计的一句话函数 def calc(n): return n**n print(calc(10)) #换成匿名函数 calc = lambda n ...

  9. python 内置函数总结(大部分)

    python 内置函数大讲堂 python全栈开发,内置函数 1. 内置函数 python的内置函数截止到python版本3.6.2,现在python一共为我们提供了68个内置函数.它们就是pytho ...

随机推荐

  1. Android OpenGL ES 开发(十): 通过GLES20与着色器交互

    1. 获取着色器程序内成员变量的id(句柄.指针) GLES20.glGetAttribLocation方法:获取着色器程序中,指定为attribute类型变量的id. GLES20.glGetUni ...

  2. Android 开发使用第三方库出现Crash时处理方案汇总

    一.Glide混淆脚本没加导致的Crash 现象描述: 使用Glide开发的时候在debug版本一直没事,但是realease版本各种Crash,报错信息如下: java.lang.IllegalAr ...

  3. 在 HTML5 中捕获音频和视频

    简介 长久以来,音频/视频捕获都是网络开发中的"圣杯".多年来,我们总是依赖于浏览器插件(Flash 或 Silverlight)实现这一点.快来看看吧! 现在轮到 HTML5 大 ...

  4. [.Net Core] 简单使用 Mvc 内置的 Ioc(续)

    简单使用 Mvc 内置的 Ioc(续) 本文基于 .NET Core 2.0. 上一章<[.Net Core] 简单使用 Mvc 内置的 Ioc>已经对日常 Mvc 中的 Ioc 的简单用 ...

  5. 关于Roll A Ball实例练习记录

    学习中不段进步! 游戏思路:通过键盘控制白色小球,让它"捡起"柠黄色方块,捡起一个加1分,全部捡起游戏胜利! 游戏对象: Ground:绿色地面 player:  小球 Obsta ...

  6. jQuery对于动态生成的元素绑定无效的问题~~

    问题:很多时候发现,对动态生成的元素绑定click事件是无效的- 原因:直接绑定到动态生成的元素是无效的,是因为Jquery扫描文档找出所有的$(‘’)元素,并把函数绑定到每个元素的click事件上, ...

  7. oracle空间索引

    1.索引创建 添加元数据 INSERT INTO USER_SDO_GEOM_METADATA (TABLE_NAME, COLUMN_NAME, DIMINFO, SRID) VALUES ('BE ...

  8. 笔记:Maven 下载和安装

    Windows 安装 下载 Apache Maven,下载地址为 http://maven.apache.org/ 解压缩下载的 ZIP 文件,复制到安装目录 增加环境变量 M2_HOME ,值为 A ...

  9. mysql主从复制安装详解

    1.环境准备:两台Linux6.8服务器,(可以通过yum安装mysql:yum install mysql mysql-server -y,安装的版本可能比较低,对于练手来说够用了 )主库ip:19 ...

  10. CXF 开发 REST 服务

    今天我们将视角集中在 REST 上,它是继 SOAP 以后,另一种广泛使用的 Web 服务.与 SOAP 不同,REST 并没有 WSDL 的概念,也没有叫做"信封"的东西,因为 ...