本文以max()为例,对min/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

初级技巧

tmp = max(1,2,4)
print(tmp)
#可迭代对象
a = [1, 2, 3, 4, 5, 6]
tmp = max(a)
print(tmp)

中级技巧:key属性的使用

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

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

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

高级技巧:找出字典中值最大的那组数据

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

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) # (123, 'B') min_prices = min(zip(prices.values(), prices.keys()))
print(min_prices) # (123, 'A')

python奇技淫巧——max/min函数的用法的更多相关文章

  1. (转)Python中的split()函数的用法

    Python中的split()函数的用法 原文:https://www.cnblogs.com/hjhsysu/p/5700347.html Python中有split()和os.path.split ...

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

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

  3. 转---python os.exec*()家族函数的用法

    execl(file, arg0,arg1,...) 用参数列表arg0, arg1 等等执行文件 execv(file, arglist) 除了使用参数向量列表,其他的和execl()相同 exec ...

  4. SQL模糊查询,sum,AVG,MAX,min函数

    cmd mysql -hlocalhost -uroot -p select * from emp where ename like '___' -- 三个横线, - 代表字符,可以查询 三个enam ...

  5. Python中的join()函数的用法

    函数:string.join() Python中有join()和os.path.join()两个函数,具体作用如下:    join():    连接字符串数组.将字符串.元组.列表中的元素以指定的字 ...

  6. python join与split函数的用法举例

    python join 和 split方法: join用来连接字符串,split恰好相反,拆分字符串的. 来看有关join.split方法的例子 1,join用法的例子 复制代码 代码示例: > ...

  7. Python中的split()函数的用法

    函数:split() Python中有split()和os.path.split()两个函数,具体作用如下:split():拆分字符串.通过指定分隔符对字符串进行切片,并返回分割后的字符串列表(lis ...

  8. Python中的join()函数的用法及列表推导式

    [红色为转载后新增部分] 函数:string.join() Python中有join()和os.path.join()两个函数,具体作用如下: join():连接字符串数组.将字符串.元组.列表中的元 ...

  9. Python中的filter()函数的用法

    转载自:脚本之家 Python内建的filter()函数用于过滤序列. 和map()类似,filter()也接收一个函数和一个序列.和map()不同的时,filter()把传入的函数依次作用于每个元素 ...

随机推荐

  1. 简述Java内存泄露

    翻译人员: 铁锚翻译时间: 2013年11月4日原文链接: The Introduction of Memory Leaks内存管理一直是Java 所鼓吹的强大优点.开发者只需要简单地创建对象,而Ja ...

  2. H5的学习之旅-H5的实体(14)

    H5有些关键字比如<等等是显示不出来的,这时候,就需要用实体来表示,实体我理解就是最初的编码 代码实例 <!DOCTYPE html> <html lang="en& ...

  3. 开源,免费和跨平台 - MVP ComCamp 2015 KEYNOTE

    2015年1月31日,作为KEYNOTE演讲嘉宾,我和来自全国各地的开发人员分享了作为一名MVP的一些体会. Keynote – Open Source, Free Tools and Cross P ...

  4. 2015/12/24:嵌入式C语言的位操作随笔

    今晚是平安夜,首先祝大家平安夜快乐,明天是圣诞,祝大家圣诞快乐!!        好了,这周都特别有空,上班也非常轻松,基本就是看看内核驱动,学学安卓,没什么正事的开发活干.今晚,我们来总结一例在现实 ...

  5. LeetCode之“树”:Sum Root to Leaf Numbers

    题目链接 题目要求: Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represe ...

  6. 【Visual C++】游戏编程学习笔记之一:五毛钱特效之透明和半透明处理

    本系列文章由@二货梦想家张程 所写,转载请注明出处. 本文章链接:http://blog.csdn.net/terence1212/article/details/44163799 作者:ZeeCod ...

  7. Learning ROS for Robotics Programming Second Edition学习笔记(六) indigo xtion pro live

    中文译著已经出版,详情请参考:http://blog.csdn.net/ZhangRelay/article/category/6506865 Learning ROS for Robotics Pr ...

  8. Linux文件与目录的默认权限与隐藏权限 - umask, chattr, lsattr, SUID, SGID, SBIT, file

    文件默认权限:umask [root@www ~]# umask 0022 <==与一般权限有关的是后面三个数字! [root@www ~]# umask -S u=rwx,g=rx,o=rx ...

  9. Java 去掉字符串中的换行符回车符等

    去掉一个字符串中的换行符.回车符等,将连续多个空格替换成一个空格 String string = "this just a test" Pattern p = Pattern.co ...

  10. vector向量容器的一些基本操作

    #include <vector> #include <iostream> using namespace std; void print(vector<int>& ...