python中的operator.itemgetter函数
operator.itemgetter函数
operator模块提供的itemgetter函数用于获取对象的哪些维的数据,参数为一些序号。看下面的例子
a = [,,]
>>> b=operator.itemgetter() //定义函数b,获取对象的第1个域的值
>>> b(a) >>> b=operator.itemgetter(,) //定义函数b,获取对象的第1个域和第0个的值
>>> b(a)
(, )
要注意,operator.itemgetter函数获取的不是值,而是定义了一个函数,通过该函数作用到对象上才能获取值。
sorted函数用来排序,sorted(iterable[, cmp[, key[, reverse]]])
其中key的参数为一个函数或者lambda函数。所以itemgetter可以用来当key的参数
a = [('john', 'A', 15), ('jane', 'B', 12), ('dave', 'B', 10)]
根据第二个域和第三个域进行排序
sorted(students, key=operator.itemgetter(1,2))
- 用 operator 函数进行多级排序
students = [('john', 'A', ), ('jane', 'B', ), ('dave', 'B', ),]
>>> sorted(students, key=itemgetter(,)) # sort by grade then by age
[('john', 'A', ), ('dave', 'B', ), ('jane', 'B', )]
## 对字典排序
>>> d = {'data1':, 'data2':, 'data3':, 'data4':}
>>> sorted(d.iteritems(), key=itemgetter(), reverse=True)
[('data4', ), ('data1', ), ('data3', ), ('data2', )]
python中的operator.itemgetter函数的更多相关文章
- 关于python中的operator.itemgetter()函数的用法
1. operator.itemgetter(num)函数 表示对对象的第num维数据进行操作获取. >>>import operator >>>a = [1, 2 ...
- 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函数用于获取对象的哪些维的数据,参数为一些序号(即需要获取的数据在对象中的序号),下面看例子. k = [,,] b = ) print(b(k)) #输 ...
- Python的operator.itemgetter函数和sorted函数
写这篇文章的目的是之前在<机器学习实战>用Python3实现KNN算法时用到的几个函数不太懂, 地址: 1- https://github.com/hitergelei/Self-Lear ...
- python--sorted函数和operator.itemgetter函数
1.operator.itemgetter函数operator模块提供的itemgetter函数用于获取对象的哪些维的数据,参数为一些序号(即需要获取的数据在对象中的序号),下面看例子. a = [1 ...
- Python中使用operator模块实现对象的多级排序
Python中使用operator模块实现对象的多级排序 今天碰到一个小的排序问题,需要按嵌套对象的多个属性来排序,于是发现了Python里的operator模块和sorted函数组合可以实现这个功能 ...
- Python中sort与sorted函数
python中列表的内置函数sort()可以对列表中的元素进行排序,而全局性的sorted()函数则对所有可迭代的序列都是适用的: 并且sort()函数是内置函数,会改变当前对象,而sorted()函 ...
- Python 函数式编程 & Python中的高阶函数map reduce filter 和sorted
1. 函数式编程 1)概念 函数式编程是一种编程模型,他将计算机运算看做是数学中函数的计算,并且避免了状态以及变量的概念.wiki 我们知道,对象是面向对象的第一型,那么函数式编程也是一样,函数是函数 ...
随机推荐
- wget在linux中安装出现错误解决办法
在使用wget命令报错 certificate common name 'xxx' doesn't match requestde host name,我们一般的解决办法是查找下载地址,但是有时候更换 ...
- Neutron: Load Balance as a Service(LBaaS)负载均衡
load balancer 负责监听外部的连接,并将连接分发到 pool member. LBaaS 有三个主要的概念: Pool Member,Pool 和 Virtual IP Pool M ...
- Quick Sort(三向切分的快速排序)(Java)
//三向切分的快速排序 //这种切分方法对于数组中有大量重复元素的情况有比较大的性能提升 public static void main(String[] args) { Scanner input ...
- laravel 多条件查询
select * from homework where (id between 1 and 10 or id between 50 and 70) and complete = 1 and (tit ...
- 前端可视化项目流程,涉及three.js(webGL),3DMax技术,持续更新
最近在做一个可视化展示的项目,记录一下流程: 建模,模型来源,可以参考沙盘展示类项目,自己建模或者拼装其他源模型(本人以前是3D建模师,可以应付一些简单的场景) 有效模型导入到web端,这里采用的ob ...
- Node.js Error: Cannot find module express的解决办法
1.全局安装express框架,cmd打开命令行,输入如下命令: npm install -g express express 4.x版本中将命令工具分出来,安装一个命令工具,执行命令: npm in ...
- 解决IOS微信浏览器底部会出现向前向后返回按钮,返回不刷新的问题
<script type="text/javascript"> //解决IOS返回页面不刷新的问题 var isPageHide = false; window.add ...
- python学习日记(继承和多态)
继承 在OOP程序设计中,当我们定义一个class的时候,可以从某个现有的class继承,新的class称为子类(Subclass),而被继承的class称为基类.父类或超类(Base class.S ...
- MySQL逻辑备份into outfile
MySQL 备份之 into outfile 逻辑数据导出(备份) 用法: select xxx into outfile '/path/file' from table_name; mysql> ...
- redis cluster简介和配置(3)
前面我介绍了 redis sentinel,既然有了sentinel,为什么还要一个cluster呢?因为随着业务量的增加,不可避免要对redis进行扩容,扩容方式一般由2种:1. 垂直扩容 2. 水 ...