python中operator.itemgetter函数
operator模块提供的itemgetter函数用于获取对象的哪些维的数据,参数为一些序号(即需要获取的数据在对象中的序号),下面看例子。
k = [,,]
b = operator.itemgetter()
print(b(k))
#输出6
k = [,,]
b = operator.itemgetter(,)
print(b(k))
#输出(, )
要注意,operator.itemgetter函数获取的不是值,而是定义了一个函数,通过该函数作用到对象上才能获取值。
students = [('john', 'C', ), ('jane', 'A', ), ('dave', 'B', )]
s = sorted(students,key = operator.itemgetter(,))
print(s)
#输出[('jane', 'A', ), ('dave', 'B', ), ('john', 'C', )]
看看下面的练习
Q:找到年龄最大的人,并输出,person = {"li":18,"wang":50,"zhang":20,"sun":22}
常规for循环解法
def fun(person):
max =
name = ""
for key,value in person.items():
if value > max:
max = value
name = key
print(name)
print(max)
fun(person)
利用operator.itemgetter函数
import operator
person = {"li":,"wang":,"zhang":,"sun":}
print(max(person.values()))
print(max(person.items(),key = operator.itemgetter())[]) # 获取最大值的 key
python中operator.itemgetter函数的更多相关文章
- Python的operator.itemgetter函数和sorted函数
写这篇文章的目的是之前在<机器学习实战>用Python3实现KNN算法时用到的几个函数不太懂, 地址: 1- https://github.com/hitergelei/Self-Lear ...
- python中operator.itemgetter
直接上例子: rs= [... {... "datetime": "Mon, 31 Aug 2015 23:00:00 GMT",... ...
- Python中的sorted函数以及operator.itemgetter函数 【转载】
operator.itemgetter函数operator模块提供的itemgetter函数用于获取对象的哪些维的数据,参数为一些序号(即需要获取的数据在对象中的序号),下面看例子. a = [1,2 ...
- Python中的sorted函数以及operator.itemgetter函数
operator.itemgetter函数operator模块提供的itemgetter函数用于获取对象的哪些维的数据,参数为一些序号(即需要获取的数据在对象中的序号),下面看例子. a = [1,2 ...
- python中的operator.itemgetter函数
operator.itemgetter函数operator模块提供的itemgetter函数用于获取对象的哪些维的数据,参数为一些序号.看下面的例子 a = [,,] >>> b=) ...
- python--sorted函数和operator.itemgetter函数
1.operator.itemgetter函数operator模块提供的itemgetter函数用于获取对象的哪些维的数据,参数为一些序号(即需要获取的数据在对象中的序号),下面看例子. a = [1 ...
- python --- Python中的callable 函数
python --- Python中的callable 函数 转自: http://archive.cnblogs.com/a/1798319/ Python中的callable 函数 callabl ...
- python中使用zip函数出现<zip object at 0x02A9E418>
在Python中使用zip函数,出现<zip object at 0x02A9E418>错误的原因是,你是用的是python2点多的版本,python3.0对python做了改动 zip方 ...
- [转载]python中multiprocessing.pool函数介绍
原文地址:http://blog.sina.com.cn/s/blog_5fa432b40101kwpi.html 作者:龙峰 摘自:http://hi.baidu.com/xjtukanif/blo ...
随机推荐
- 解决nginx中fastcgi(php-fpm)60s超时的问题
在配置中加上 location ~ .*\.(php|php5)?$ { fastcgi_pass 127.0.0.1:9000; fastcgi_read_timeout 700; fastcgi_ ...
- 使用CreateProcess创建新的process 并返回process运行结束返回值
转自:http://blog.csdn.net/zgl7903/article/details/5975284 转载这篇主要是记住:获得create的新进程运行结束时的返回值的方法 如下: #in ...
- Win8设计——现代设计,可使你的应用脱颖而出的元素
Microsoft 设计准则 Windows 在现代设计方面遥遥领先.它采用了“真实数字”原则并从瑞士风格和交通枢纽的寻路系统中汲取灵感. 阅读详细信息 设计元素 动态磁贴 动态磁贴向你提供了一个独特 ...
- 告知你不为人知的UDP-连接性和负载均衡
版权声明:本文由黄日成原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/812444001486438028 来源:腾云阁 h ...
- Android调试技巧
转自:http://gityuan.com/2017/07/11/android_debug/ 一. 获取Trace 调用栈信息(Trace)是分析异常经常使用的,这里简单划分两类情况: 当前线程Tr ...
- 使用 mysql workbench 建议
在日常使用mysql workbench时,未免操作失误,不建议启用远程管理.
- git 推送出现 "fatal: The remote end hung up unexpectedly"
原因:原因是推送的文件太大 解决方案: 注意,有时候会看不到.git文件,可能被隐藏了,在这里勾选上隐藏的项目,就可以看到了. 第一种,全局设置 在C:\Users\wang\git\.git\con ...
- VUE单独页面body css设置
使用created周期用JS来处理BODY的样式 export default { beforeCreate: function () { document.getElementsByTagName( ...
- Python并行编程的几个要点
一.基于线程的并行编程 如何使用Python的线程模块 如何定义一个线程 如何探测一个线程 如何在一个子类中使用线程 Lock和RLock实现线程同步 信号实现线程同步 条件(condition)实现 ...
- Gradle 教程
extends:http://www.zhihu.com/question/27866554/answer/38427122 stormzhang博客精华 有一个Gradle 的教程,足够你入门啦. ...