【386】operator 的 itemgetter、slice、and_、or_
itemgetter
用来获取数组中指定索引的元素
from operator import itemgetter
itemgetter(1, 3, 5)('ABCDEFG') output:
('B', 'D', 'F')
slice
用来为列表切片,也是获取指定索引的元素
a = slice(0,10,2)
b = list(range(10))
b[a]
b[0:10:2]
itemgetter(a)(b) output:
[0, 2, 4, 6, 8]
[0, 2, 4, 6, 8]
[0, 2, 4, 6, 8]
and_ 相当于 a & b,用来求两个集合的交集
可以联合 reduce 实现多个集合交集的计算
from functools import reduce
from operator import and_
print(reduce(and_, [{1,2,3},{2,3,4},{3,4,5},{1,3,5}])) output:
{3}
or_ 相当于 a | b,用来求两个集合的交集
可以联合 reduce 实现多个集合并集的计算
from functools import reduce
from operator import or_
print(reduce(or_, [{1,2,3},{2,3,4},{3,4,5},{1,3,5}])) output:
{1, 2, 3, 4, 5}
【386】operator 的 itemgetter、slice、and_、or_的更多相关文章
- operator的itemgetter和attrgetter
前几天在给个list做排序的时候,隐隐约约想起来有个语法糖可以替代lambda函数,用来获取list中dict的key,作为排序的key. 这个语法糖平时用得少,怎么都想不起来.今天查看python标 ...
- python的operator.itemgetter('click')用于定义获取'click'项的函数
python的排序参见文章http://blog.csdn.net/longshenlmj/article/details/12747195 这里介绍 import operator模块 operat ...
- operator.itemgetter() 字典列表排序
## 字典列表排序 students = [ {"name": "Stanley", "age": 22, "score" ...
- 【Python】operator 模块简单介绍
简单介绍几个常用的函数,其他的请参考文档. operator.concat(a, b) **operator.__concat__(a, b)** 对于 a.b序列,返回 a + b(列表合并) -- ...
- Python之operator库
operator库常用方法 Function Syntax add(a+b) a + b concat(seq1,seq2) seq1 + seq2 contains(seq, obj) obj in ...
- Python模块:operator简单介绍
Python官方文档地址:https://docs.python.org/3.6/library/operator.html?highlight=operator Operator提供的函可用于对象比 ...
- operator模块和functools模块
operator模块 在函数式编程中,经常需要把算术运算符当作函数使用.例如,不使用 递归计算阶乘.求和可以使用 sum 函数,但是求积则没有这样的函数. 我们可以使用 reduce 函数(5.2.1 ...
- operator模块常见方法介绍
operator.concat(a, b) 对于 a.b序列,返回 a + b(列表合并) --------------------------------- operator.countOf(a, ...
- 飘逸的python - 多条件排序及itemgetter的应用
曾经客户端的同事用as写一大堆代码来排序,在得知python排序往往只需要一行,惊讶无比,遂对python产生浓厚的兴趣. 之前在做足球的积分榜的时候需要用到多条件排序,如果积分相同,则按净胜球,再相 ...
随机推荐
- android studio AIDL 编译时 错误:找不到符号
原贴路径:http://blog.csdn.net/baidu_30164869/article/details/51036405 PS:在android studio中 当将java文件放到aidl ...
- Find the peace with yourself
The purpose of being mature is to find the real calm and peace with yourself. Or you can say the tur ...
- Meet in the middle学习笔记
Meet in the middle(MITM) Tags:搜索 作业部落 评论地址 PPT中会讲的很详细 当搜索的各项互不影响(如共\(n\)个物品前\(n/2\)个物品选不选和后\(n/2\)个物 ...
- Android图片变形,ImageView属性的设置。
<ImageView android:id="@+id/iv" android:layout_width="match_parent" android:l ...
- SP1812 LCS2 - Longest Common Substring II
能匹配上子串的节点对它的所有parent都有贡献 在树上转移即可 #include<cstdio> #include<algorithm> #include<cstrin ...
- Python ————反射机制
python中的反射功能是由以下四个内置函数提供:hasattr.getattr.setattr.delattr,改四个函数分别用于对对象内部执行:检查是否含有某成员.获取成员.设置成员.删除成员. ...
- 为DOM节点添加或者删除class
项目中如果应用了常用的javascript类库,多数情况下,这些已经封装好的类库,都会封装一个类似于addClass和removeClass的方法,以便于我们对DOM节点的class进行操作. 以jQ ...
- 五大排序算法(Python)
冒泡排序 冒泡排序通常是在CS入门课程中教的,因为它清楚地演示了排序是如何工作的,同时又简单易懂.冒泡排序步骤遍历列表并比较相邻的元素对.如果元素顺序错误,则交换它们.重复遍历列表未排序部分的元素,直 ...
- Java 递归详解
递归详解: 1.递归一句话通俗讲就是一个方法自动重复调用自己的过程. 2.因为是重复调用自己了,所以看起来像一个循环,所以为了避免内存溢出系统崩溃,我们需要在方法里加一个返回值判断,用于递归循环的跳出 ...
- Microsoft Office Access数据库或项目包含一个对文件“dao360.dll”版本5.0.的丢失的或损坏的引用。
今天使用 office 2007 access 打开 2003 的数据库中的表时候,提示这个错误.经过搜索,发现是没有 dao360.dll 的问题. 在 https://cn.dll-files.c ...