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函数的更多相关文章

  1. Python的operator.itemgetter函数和sorted函数

    写这篇文章的目的是之前在<机器学习实战>用Python3实现KNN算法时用到的几个函数不太懂, 地址: 1- https://github.com/hitergelei/Self-Lear ...

  2. python中operator.itemgetter

    直接上例子: rs=  [...     {...       "datetime": "Mon, 31 Aug 2015 23:00:00 GMT",...  ...

  3. Python中的sorted函数以及operator.itemgetter函数 【转载】

    operator.itemgetter函数operator模块提供的itemgetter函数用于获取对象的哪些维的数据,参数为一些序号(即需要获取的数据在对象中的序号),下面看例子. a = [1,2 ...

  4. Python中的sorted函数以及operator.itemgetter函数

    operator.itemgetter函数operator模块提供的itemgetter函数用于获取对象的哪些维的数据,参数为一些序号(即需要获取的数据在对象中的序号),下面看例子. a = [1,2 ...

  5. python中的operator.itemgetter函数

    operator.itemgetter函数operator模块提供的itemgetter函数用于获取对象的哪些维的数据,参数为一些序号.看下面的例子 a = [,,] >>> b=) ...

  6. python--sorted函数和operator.itemgetter函数

    1.operator.itemgetter函数operator模块提供的itemgetter函数用于获取对象的哪些维的数据,参数为一些序号(即需要获取的数据在对象中的序号),下面看例子. a = [1 ...

  7. python --- Python中的callable 函数

    python --- Python中的callable 函数 转自: http://archive.cnblogs.com/a/1798319/ Python中的callable 函数 callabl ...

  8. python中使用zip函数出现<zip object at 0x02A9E418>

    在Python中使用zip函数,出现<zip object at 0x02A9E418>错误的原因是,你是用的是python2点多的版本,python3.0对python做了改动 zip方 ...

  9. [转载]python中multiprocessing.pool函数介绍

    原文地址:http://blog.sina.com.cn/s/blog_5fa432b40101kwpi.html 作者:龙峰 摘自:http://hi.baidu.com/xjtukanif/blo ...

随机推荐

  1. WP8.1学习系列(第二十二章)——在页面之间导航

    在本文中 先决条件 创建导航应用 Frame 和 Page 类 页面模板中的导航支持 在页面之间传递信息 缓存页面 摘要 后续步骤 相关主题 重要的 API Page Frame Navigation ...

  2. c++ istream转换为std::string

    std::istreambuf_iterator<char> eos; std::string s(std::istreambuf_iterator<char>(stream) ...

  3. LeetCode 29 Divide Two Integers (不使用乘法,除法,求模计算两个数的除法)

    题目链接: https://leetcode.com/problems/divide-two-integers/?tab=Description   Problem :不使用乘法,除法,求模计算两个数 ...

  4. Linux 下 SVN 命令操作详解

    1.将文件checkout到本地目录 svn checkout path(path是服务器上的目录)    例如:svn checkout svn://192.168.1.1/pro/domain   ...

  5. [Sdoi2016]齿轮

    4602: [Sdoi2016]齿轮 Time Limit: 10 Sec  Memory Limit: 512 MB Submit: 613  Solved: 324 [Submit][Status ...

  6. apache反向代解决绝对路径可能出现的问题

    Apache2在对应用做反向代理的时候,如果应用的页面中引用的连接或者是资源的地址是相对地址的话,是没有问题的,当应用中引用的地址是绝对地址(比如:)或者是request.getContextPath ...

  7. 剑指offer题目记录

    1.如下为类型CMyString的声明,请为该类型添加赋值运算符函数. class CMyString { public: CMyString(char* pData = NULL); CMyStri ...

  8. http后台json解析实例

    localhost:8080/hbinterface/orderInterface/sIReverseAccept.do?bizType=4&&bnetAccount=ESBTEST2 ...

  9. Thymeleaf 入门

    基本项目结构: Thymeleaf配置: spring.thymeleaf.mode=LEGACYHTML5 spring.thymeleaf.cache=false spring.thymeleaf ...

  10. Elasticsearch-mapper 基于注解方式生成mapping(2.0以上)

    Elasticsearch生成mapping的方式上有多种方式,我们可以把mapping做成配置文件,也可以用spring-data-elasticsearch基于注解生成. 在基于注解生成这种方式上 ...