内置函数——max

Python max内置函数

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()

内置函数:max 用法的更多相关文章

  1. freemarker内置函数和用法

    原文链接:http://www.iteye.com/topic/908500 在我们应用Freemarker 过程中,经常会操作例如字符串,数字,集合等,却不清楚Freemrker 有没有类似于Jav ...

  2. [SQL]SUTFF内置函数的用法 (删除指定长度的字符并在指定的起始点插入另一组字符)

    STUFF 删除指定长度的字符并在指定的起始点插入另一组字符. 语法 STUFF ( character_expression , start , length , character_express ...

  3. enumerate next eval reload 内置函数的用法

    enumerate next eval reload 内置函数的用法 #enumerate() 函数用于将一个可遍历的数据对象(如列表.元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用 ...

  4. Python内置函数reversed()用法分析

    Python内置函数reversed()用法分析 这篇文章主要介绍了Python内置函数reversed()用法,结合实例形式分析了reversed()函数的功能及针对序列元素相关操作技巧与使用注意事 ...

  5. $Python常用内置函数典型用法

    Python中有许多功能丰富的内置函数,本文基于Python 2.7,就常用的一些函数的典型用法做一些积累,不断更新中. sorted函数的三种用法 # coding:utf-8 # sorted函数 ...

  6. 面向对象编程之super内置函数的用法

    先来看一段代码: 定义一个名叫People的父类,又定义了一个叫Teacher的老师类和一个叫Student的学生类 来继承People的类,并根据这两个子类实例化出两个对象s1和t1. class ...

  7. 内置函数-max、min、round、sorted、ord、chr、any、all、dir、eval、exec、map、filter、reduce

    http://www.nnzhp.cn/archives/152 1.max,min,round print(max([3,4.563,3,6,2.5])) #取最大值,可循环参数即可,int类型的, ...

  8. [SQL]SUTFF内置函数的用法

    STUFF 删除指定长度的字符并在指定的起始点插入另一组字符. 语法 STUFF ( character_expression , start , length , character_express ...

  9. python内置函数getattr用法

    class Tests(object):    #定义类     aaa = '10'          #定义变量       def test(self):     #定义类的方法test     ...

随机推荐

  1. CefSharp.WinForms

    CefSharp.WinForms 一.  前言 银医通项目,现在另外一家医院需要上系统,所以项目需要重新搭建,由于这家医院的His系统和另外一家医院的His系统不同,界面风格也不一致,所以重新搭建, ...

  2. 【C#系列】你应该知道的委托和事件

    [C#系列]你应该知道的委托和事件   本篇文章更适合具有一定开发经验,一定功底,且对底层代码有所研究的朋友!!! 本篇文章主要采用理论和代码实例相结合方式来论述委托和事件,涉及到一些边界技术,如软件 ...

  3. 用Jmeter测试RabbitMQ

    1.下载AMQP插件 github上面有源码,可以通过ant+ivy在本地进行打包(下载IDEA实践成功) https://github.com/jlavallee/JMeter-Rabbit-AMQ ...

  4. mysql调用存储过程出现Illegal mix of collations错误

    执行sql语句正常 执行存储过程 异常 提示 Illegal mix of collations (utf8_general_ci,IMPLICIT) and (utf8_unicode_ci,IMP ...

  5. 一些移动端的ui框架

    一些移动端的ui框架 https://jqweui.cn/resource

  6. Linux Tools

    WinSCP  http://winscp.net/eng/download.php Xshell 5

  7. docker下部署spring boot

    第 5 章 Docker + Spring Boot: 快速搭建和部署Java Web应用 0.你需要: JDK 1.8 : java -version Maven 3.0+ : mvn -v Git ...

  8. springboot解决第三方依赖jar包的问题

    公司现在用的是springboot+maven,想要把一些老的项目都改成这种框架.但是一些老的项目中有好多第三方的jar包或者是自己的jar包,maven库上没有.最初的解决方案是一个个的deploy ...

  9. Git使用技巧(1)-- 配置【持续更新】

    配置名字和邮箱 git config --global user.name "Your Name" git config --global user.email "ema ...

  10. request:getParameter和getAttribute区别

    getParameter 是用来接受用post个get方法传递过来的参数的.getAttribute 必须先setAttribute.(1)request.getParameter() 取得是通过容器 ...