Python_内置函数之max
源码:
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的更多相关文章
- python_内置函数
#内置函数 #1.abs 获取绝对值 # abs(-10) # --->10 # # abs(10) # --->10 # # abs(0) # --->0 #2.all() 参数为 ...
- Python_内置函数2_44
字符串类型代码执行: exec('print(123)') eval('print(123)') print(eval('1*2+3+4')) # 有返回值 print(exec('1+2+3+4') ...
- python_内置函数1_42
内置函数 内置函数大全: Built-in Functions abs() dict() help() min() setattr() all() dir() hex() next() ...
- 内置函数:max 用法
内置函数——max Python max内置函数 max(iterable, *[, key, default]) max(arg1, arg2, *args[, key]) Return the l ...
- Python_内置函数和匿名函数
楔子 在讲新知识之前,我们先来复习复习函数的基础知识. 问:函数怎么调用? 函数名() 如果你们这么说...那你们就对了!好了记住这个事儿别给忘记了,咱们继续谈下一话题... 来你们在自己的环境里打印 ...
- Python内置函数(3)——max
英文文档: max(iterable, *[, key, default]) max(arg1, arg2, *args[, key]) Return the largest item in an i ...
- Python内置函数(41)——max
英文文档: max(iterable, *[, key, default]) max(arg1, arg2, *args[, key]) Return the largest item in an i ...
- Python_内置函数之zip
zip函数用于将可迭代的对象作为参数,将对象中的元素打包成一个个元祖,然后返回这些元祖组成的列表.如果各个迭代器的元素个数不一致,则返回列表长度与最短的对象相同. l1 = [1, 2, 3] l2 ...
- Python_内置函数之map()
map 会根据提供的函数对指定序列做映射. 代码如下: def square(x): return x ** 2 ret = map(square, [1, 2, 3, 4, 5]) # 计算列表各元 ...
随机推荐
- ADV190007 - “PrivExchange” 特权提升漏洞的指南
Microsoft Exchange Server中存在一个特权提升漏洞.成功利用此漏洞的攻击者可能会尝试模仿Exchange服务器的任何其他用户.要利用此漏洞,攻击者需要执行中间人攻击才能将身份验证 ...
- Flask中的CBV
Flask中的CBV 第一种 class Index(views.MethodView): methods = ['GET', 'POST'] decorators = [] def get(self ...
- python中的猴子补丁Monkey Patch
python中的猴子补丁Monkey Patch 什么是猴子补丁 the term monkey patch only refers to dynamic modifications of a cla ...
- 持续集成-Jenkins常用插件安装
1. 更新站点修改 由于之前说过,安装Jenkins后首次访问时由于其他原因[具体未知]会产生离线问题.网上找了个遍还是不能解决,所以只能跳过常用插件安装这步.进入Jenkins后再安装这些插件. 在 ...
- 发现一种写法,类似callback&&callback()
与callback&&callback()异曲同工 return data && { title: `To-do (${data.length})`, componen ...
- cf 20190307 Codeforces Round #543 (Div. 2, based on Technocup 2019 Final Round)
B. Mike and Children time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- HTTP请求报文解剖
转自:https://www.iteye.com/topic/1124408 HTTP请求报文由3部分组成(请求行+请求头+请求体): 下面是一个实际的请求报文: ①是请求方法,GET和POST是最常 ...
- [LeetCode]最大系列(最大正方形221,最大加号标志764)
221. 最大正方形 题目描述: 在一个由 0 和 1 组成的二维矩阵内,找到只包含 1 的最大正方形,并返回其面积. 示例: 输入: 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 ...
- 彻底关掉MyEclipse的自动校验,特别是对js文件的校验!!
百度搜出来的一大堆方法都没有用,因为他们都是一样的,让你关掉校验:Window -->Preferences -->MyEclipse -->单击Validation. 但是还是没用 ...
- python textwrap的使用
参考:https://docs.python.org/3.6/library/textwrap.html textwrap模块提供了一些方便的函数,以及TextWrapper类,它执行所有的工作.如果 ...