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 ...
随机推荐
- 《转载》WIN10 64位系统 32位Python2.7 PIL安装
http://blog.csdn.net/kanamisama0/article/details/53960281 首先安装这个真的出了好多问题,之前装过一次PIL也失败了,就一直没管,今天刚好找了机 ...
- Glide加载图片缓存库出现——You cannot start a load for a destroyed activity
请记住一句话:不要再非主线程里面使用Glide加载图片,如果真的使用了,请把context参数换成getApplicationContext.
- 如何在浏览器中简单模拟微信浏览器(仅限于通过User Agent进行判断的页面)
模拟微信浏览器: .打开360极速 .F12开发者工具 .开发者模式左上方有一个手机样子的图标 点击进入 设备模式‘ .将UA选项中的字符串替换成: Mozilla/ 备注: 替换的字符串是微信浏览器 ...
- 跨域 - jsonp轻松搞定跨域请求
1.jsonp轻松搞定跨域请求 vue中使用axios,遇到跨域我就蒙逼了.第一次真正意义上的尝试使用jsonp js中用 var myscript = document.createElement( ...
- Egret 中实现3种状态切换按钮
一.游戏中的常用3种状态按钮 Egret种提供了2种状态切换的按钮ToggleButton. 但是在游戏中常用到3种状态的按钮,比如任务系统的领取.已领取.未领取. 比如下图中宝箱的打开.浏览后打开. ...
- [Windows] 使用SC 命令管理与配置服务
sc config wuauserv start= demand sc config wuauserv start= disabled
- Gradle gitignore Gradle 模式 上传SVN 要忽略的文件
.gradle /local.properties /.idea/workspace.xml /.idea/libraries .DS_Store /build /captures /.idea *. ...
- 9.7 Django 书单列表页面
昨天的迭代版本,增加了编辑出版社,编辑列表,增添了返回页面! 具体的看 github : https://github.com/TrueNewBee/pythonDemo 看一下效果图: 整体来说还是 ...
- mysql limit 优化
1.当取出的数据超过20%时,优化器不会使用索引,而是全表扫描: 2.limit和offset的问题,其实是offset的问题,它会导致mysql扫描大量不需要的行然后删掉 如: select * f ...
- Linux下 磁盘扩容的两种方式
Hadoop扩容 概述 Hadoop存储容量或计算能力不能满足日益增长的需求时,就需要扩容. 扩容有两个方案: 1) 增加磁盘 2) 增加节点 方案一:扩大虚拟磁盘 扩大容量 将虚拟的Linux关闭, ...