【转】Python max内置函数详细介绍
#max() array1 = range(10)
array2 = range(0, 20, 3)
print('max(array1)=', max(array1))
print('max(array2)=', max(array2))
print('max(array1,)=', max(array1, key=lambda x: x > 3) )
print(max(1, 2))
print(max('ah', 'bf', key=lambda x: x[1]))
print(max(array1, array2, key=lambda x: x[1])) def comparator(x):
return x[2]
print(max('ah2', 'bf3', key=comparator)) 结果输出如下:
max(array1)= 9
max(array2)= 18
max(array1,)= 4
2
ah
range(0, 20, 3)
bf3
说明:
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('1234') # 传入1个可迭代对象,取其最大元素值
'4'
3. 当传入参数为数据类型不一致时,传入的所有参数将进行隐式数据类型转换后再比较,如果不能进行隐式数据类型转换,则会报错。
>>> max(1,1.1,1.3E1) # 整数与浮点数可取最大值
13.0
>>> max(1,2,3,'3') # 数值与字符串不能取最大值
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
max(1,2,3,'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,'3') #数值和字符串不能取最大值
Traceback (most recent call last):
File "<pyshell#21>", line 1, in <module>
max(1,2,'3')
TypeError: unorderable types: str() > int()
>>> max(1,2,'3',key = int) # 指定key为转换函数后,可以取最大值
'3' >>> 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()
原文地址:http://www.jb51.net/article/97571.htm
【转】Python max内置函数详细介绍的更多相关文章
- python内置函数详细介绍
知识内容: 1.python内置函数简介 2.python内置函数详细介绍 一.python内置函数简介 python中有很多内置函数,实现了一些基本功能,内置函数的官方介绍文档: https: ...
- Python中内置函数的介绍
内置函数的功能介绍 常用内置函数如下: 1.abs() 绝对值 格式:abs(x) 例如:print(abs(-18)) >>> 18 返回值:number #该函数主要用于数值类的 ...
- linux awk 内置函数详细介绍(实例)
这节详细介绍awk内置函数,主要分以下3种类似:算数函数.字符串函数.其它一般函数.时间函数 一.算术函数: 以下算术函数执行与 C 语言中名称相同的子例程相同的操作: 函数名 说明 atan2( y ...
- linux awk 内置函数详细介绍(实例)
这节详细介绍awk内置函数,主要分以下3种类似:算数函数.字符串函数.其它一般函数.时间函数 一.算术函数: 以下算术函数执行与 C 语言中名称相同的子例程相同的操作: 函数名 说明 atan2( y ...
- python基础-内置函数详解
一.内置函数(python3.x) 内置参数详解官方文档: https://docs.python.org/3/library/functions.html?highlight=built#ascii ...
- 如何查看Python的内置函数
经常调用的时候不知道python当前版本的内置函数是哪些,可以用下面的指令查看: C:\Users\Administrator>python Python 2.7.11 (v2.7.11:6d1 ...
- python基础——内置函数
python基础--内置函数 一.内置函数(python3.x) 内置参数详解官方文档: https://docs.python.org/3/library/functions.html?highl ...
- Python的内置函数
python的内置函数一共有68个,下面将简单介绍各个函数的功能. abs() dict() help() min() setattr() all() dir() hex() next() slice ...
- Python入门-内置函数一
什么是内置函数?就是python给你提供的拿来直接用的函数,比如print,input等等,截止到python版本3.6.2 python一共提供了68个内置函数,他们就是python直接提供给我们的 ...
随机推荐
- C#虚方法、抽象类、方法重写
Timer.每隔一段时间触发一个事件.不可视控件.Interval.Enabled.Tick事件.计量单位:ms(毫秒). 1秒=1000毫秒.取当前时间 DateTime.Now.ToSt ...
- c++ why can't class template hide its implementation in cpp file?
类似的问题还有: why can't class template use Handle Class Pattern to hide its implementation? || why there ...
- DataUml Design 介绍8-DataUML 1.2版本正式发布(支持SQLite数据库、NetUML开发框架)
DataUML 1.2版本在软件架构上有了很大的变化,目前DataUML支持Access.SQLite.MY SQL .ORACLE.MS SERVER2000.MS SERVER2005.MS SE ...
- MII接口概念简述
MII: Medium Independent Interface 媒体独立接口,也称介质无关接口 RMII: Reduced MII 简化媒体独立接口 GMII: Gigabit Medium In ...
- LeetCode406. Queue Reconstruction by Height Add to List
Description Suppose you have a random list of people standing in a queue. Each person is described b ...
- redis 管道
http://www.w3cschool.cc/redis/redis-pipelining.html
- htmt 5 素材
http://www.html5code.net/plus/list.php?tid=20
- nginx配置后只有根目录首页index.php能访问,其他页面404
只有首页面根目录可以访问,其他页面地址都是404 not found.网上找了半天url重定向,url重写都试了无效,要不就是重定向过多,下图为跳坑历程. location / { #if ($htt ...
- No image!使用border-color属性来制作小三角形
border属性在项目中使用的还是蛮频繁的.例如页签.按钮这样的. border简写属性是按照如下属性设置的: border:border-width/border-style/border-colo ...
- 【BZOJ4821】[Sdoi2017]相关分析 线段树
[BZOJ4821][Sdoi2017]相关分析 Description Frank对天文学非常感兴趣,他经常用望远镜看星星,同时记录下它们的信息,比如亮度.颜色等等,进而估算出星星的距离,半径等等. ...