源码:

 def max(*args, key=None): # known special case of max
"""
max(iterable, *[, default=obj, key=func]) -> value
max(arg1, arg2, *args, *[, key=func]) -> value With a single iterable argument, return its biggest item. The
default keyword-only argument specifies an object to return if
the provided iterable is empty.
With two or more arguments, return the largest argument.
"""
pass

简单使用:

ret = max(1, 2, 4)
print(ret)

  结果:

4

  

一般使用:

当key不为空时,就以key的函数对象为判断的标准.

如果我们想找出一组数中绝对值最大的书,就可以配合lambda先进行处理,再找出最大值.

a = [-9, -8, 1, 3, -4, 6]
ret = max(a, key=lambda x: abs(x))
print(ret)

  结果:

-9

骚操作:找出字典中值最大的那组数据

  如果有一组商品,其名称和价格都存在一个字典中,可以用下面的方法快速找到价格最贵的那组商品:

prices = {
'A': 123,
'B': 450.1,
'C': 12,
'E': 444
}
# 在对字典进行数据操作的时候,默认值会处理key,而不是value
# 先使用zip把字典的keys和values翻转过来,再用max取出值最大的那组数据
max_prices = max(zip(prices.values(), prices.keys()))
print(max_prices)

  结果:

(450.1, 'B')

  当字典中的value相同的时候,才会比较key:

prices = {
'A': 123,
'B': 123
}
max_prices = max(zip(prices.values(), prices.keys()))
print(max_prices)
min_prices = min(zip(prices.values(), prices.keys()))
print(min_prices)

  结果:

(123, 'B')
(123, 'A')

  

dic = {
'k1': 10,
'k2': 100,
'k3': 30
}
print(max(dic))
print(dic[max(dic, key= lambda k: dic[k])])

  结果:

k3
100

  

Python_内置函数之max的更多相关文章

  1. python_内置函数

    #内置函数 #1.abs 获取绝对值 # abs(-10) # --->10 # # abs(10) # --->10 # # abs(0) # --->0 #2.all() 参数为 ...

  2. Python_内置函数2_44

    字符串类型代码执行: exec('print(123)') eval('print(123)') print(eval('1*2+3+4')) # 有返回值 print(exec('1+2+3+4') ...

  3. python_内置函数1_42

    内置函数 内置函数大全:     Built-in Functions     abs() dict() help() min() setattr() all() dir() hex() next() ...

  4. 内置函数:max 用法

    内置函数——max Python max内置函数 max(iterable, *[, key, default]) max(arg1, arg2, *args[, key]) Return the l ...

  5. Python_内置函数和匿名函数

    楔子 在讲新知识之前,我们先来复习复习函数的基础知识. 问:函数怎么调用? 函数名() 如果你们这么说...那你们就对了!好了记住这个事儿别给忘记了,咱们继续谈下一话题... 来你们在自己的环境里打印 ...

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

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

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

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

  8. Python_内置函数之zip

    zip函数用于将可迭代的对象作为参数,将对象中的元素打包成一个个元祖,然后返回这些元祖组成的列表.如果各个迭代器的元素个数不一致,则返回列表长度与最短的对象相同. l1 = [1, 2, 3] l2 ...

  9. Python_内置函数之map()

    map 会根据提供的函数对指定序列做映射. 代码如下: def square(x): return x ** 2 ret = map(square, [1, 2, 3, 4, 5]) # 计算列表各元 ...

随机推荐

  1. logstash配置

    input { #You must define a [type], otherwise you cannot get a field to cut. tcp { port => 5045 ty ...

  2. 关于cisco日志的配置

    实例: en conf t clock timezone GMT+8  #设置北京时间 exit clock set HH:MM:SS DAY MONTH YEAR  #设置当前时间 service ...

  3. LeetCode算法题-Intersection of Two Arrays II(Java实现)

    这是悦乐书的第208次更新,第220篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第76题(顺位题号是350).给定两个数组,编写一个函数来计算它们的交集.例如: 输入: ...

  4. June 9. 2018, Week 23rd, Saturday

    I know nothing except the fact of my ignorance. 除了自己的无知,我一无所知. Believe it or not, true wisdom exists ...

  5. 文件IO模型

    In case of kernel-space network drivers, all three regions are mapped to kernel space, and any acces ...

  6. [1] YOLO 图像检测 及训练

    YOLO(You only look once)是流行的目标检测模型之一, 原版 Darknet 使用纯 C 编写,不需要安装额外的依赖包,直接编译即可. CPU环境搭建 (ubuntu 18.04) ...

  7. Mqtt用户认证

    http://emqtt.com/docs/v2/guide.html 1默认是匿名认证,不用输入用户名和密码,直接可连接 2如何开启用户名和密码认证模式 2-1关闭匿名认证 在你的MQTT安装目录下 ...

  8. ORA-01034:ORACLE not available ORA-27101:shared memory realm does not exit

    ORA-01034:ORACLE not available ORA-27101:shared memory realm does not exit   ERROR: ORA-01034:ORACLE ...

  9. 解决 Vim 的 quickfix 插件错误信息乱码问题

      将以下代码插入 vim 配置文件即可,       function! QfMakeConv()        let qflist = getqflist()        for i in q ...

  10. robotframework日志中文乱码,编译提示‘utf-8’ codecxxxx。

    1.需要设置robotframework的语系 2.设置完后,需要重启robotframework才生效.它比较特别,什么改变都要重启才生效