Python中map,filter,reduce,zip的应用
事例1:
l=[('main', 'router_115.236.xx.xx', [{'abc': 1}, {'dfg': 1}]),
('main', 'router_183.61.xx.xx', [{'abc': 0}, {'dfg': 1}]),
('main', 'router_52.11.xx.xx', [{'abc': 0}, {'dfg': 1}]),
('main', 'router_183.17.xx.xx', [{'abc': 1}, {'dfg': 1}])
]
检查参数l列表中所有item的第二项([{'abc': 1}, {'dfg': 1}])是否相等
for的做法
a=l[0][2]
flag=1
for i in l:
if a==i[2]:
continue
else:
flag=0
break
高级的做法:
a=map(lambda x:x[2],l)
r=reduce(lambda x,y:x if x==y else False,a)
事例2:
a=[{'a':1},{'b':11},{'c':12},{'d':11}]
b=[{'e':14},{'f':17},{'g':18},{'h':41}]
把所以数据合并成一个字典
一般的做法:
a=a+b
c={}
for i in a:
c.update(i)
print c
高级的做法
def _func(x,y):
x.update(y)
return x
c=reduce(_func,a)
事例3:
取出一个列表中重复的元素
a = [1,2,3,4,4,5,5,6,9]
b=filter(lambda x:True if a.count(x)>1 else False,a)
c=list(set(b))
输出结果为 [4,5]
Python中map,filter,reduce,zip的应用的更多相关文章
- 【转】Python 中map、reduce、filter函数
转自:http://www.blogjava.net/vagasnail/articles/301140.html?opt=admin 介绍下Python 中 map,reduce,和filter 内 ...
- python常用函数进阶(2)之map,filter,reduce,zip
Basic Python : Map, Filter, Reduce, Zip 1-Map() 1.1 Syntax # fun : a function applying to the iterab ...
- Python面试题之Python中的lambda map filter reduce zip
当年龟叔想把上面列出来的这些都干掉.在 “All Things Pythonic: The fate of reduce() in Python 3000”这篇文章中,他给出了自己要移除lambda. ...
- Python中Lambda, filter, reduce and map 的区别
Lambda, filter, reduce and map Lambda Operator Some like it, others hate it and many are afraid of t ...
- python中map()和reduce()的使用
map() 会根据提供的函数对指定序列做映射.map(function, iterable, ...)Python 3.x 返回迭代器.print(map()) 返回迭代器地址一般和list一起用 才 ...
- Python中map和reduce函数??
①从参数方面来讲: map()函数: map()包含两个参数,第一个是参数是一个函数,第二个是序列(列表或元组).其中,函数(即map的第一个参数位置的函数)可以接收一个或多个参数. reduce() ...
- python中map、reduce函数
map函数: 接受一个函数 f 和一个 list .格式:map( f , L),对L中的每个元素,进行f(x)的一个操作. 例如,对于list [1, 2, 3, 4, 5, 6, 7, 8, 9] ...
- Python中map和reduce函数
①从参数方面来讲: map()函数: map()包含两个参数,第一个是参数是一个函数,第二个是序列(列表或元组).其中,函数(即map的第一个参数位置的函数)可以接收一个或多个参数. reduce() ...
- Python函数式编程中map()、reduce()和filter()函数的用法
Python中map().reduce()和filter()三个函数均是应用于序列的内置函数,分别对序列进行遍历.递归计算以及过滤操作.这三个内置函数在实际使用过程中常常和“行内函数”lambda函数 ...
随机推荐
- 【ZT】修复iCloud中查找我的iPhone、查找我的iPad无法显示地图的方法
http://blog.sina.com.cn/s/blog_4ff28d30010118cm.html 进入C:\Windows\System32\drivers\etc在hosts文件里加入如下地 ...
- Tips: compilation and creating new projects on Android 4.0
If you have downloaded android 4.0, you should read the article. $ANDROID_ROOT/tools/android.bat is ...
- HDU1009 FatMouse' Trade
FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- [原创] 对于深度学习(deep learning)在工业界的应用现状和突破 [by matthewbai]
现状: 1. 目前大家对于大部分需求,通常采用multiple layer,units in each layer也是人工订好的(虽然可以做稀疏,但是在same layer范围内竞争). 2. 网络结 ...
- SerialPort基本小例
SerialPort是用于串口通信的控件与VB6中的MSCOMM控件相似,使用很方便... vb.net CodeImports System.IO.PortsImports System.TextP ...
- iOS之SDWebImage的使用
第一步,下载SDWebImage,导入工程.github托管地址https://github.com/rs/SDWebImage 第二步,在需要的地方导入头文件 1 #import "UII ...
- OWASP 2013年十大Web应用安全漏洞
权威的安全组织OWASP 更新了Top 10:https://www.owasp.org/index.php/Top_10_2013-Top_10 十大安全漏洞分别是:1. 注入,包括SQL.操作系统 ...
- mysql同时向一个表中插入多条数据问题!!见详细
INSERT INTO `表名` (`字段1`,`字段2`,`字段3`,`字段4`) values ('数组1数据1','数组1数据2','数组1数据3','数组1数据4'), ('数组2数据1',' ...
- Open Flash Chart在php中的使用教程
http://www.cnblogs.com/huangcong/archive/2013/01/27/2878650.html 为了画一个漂亮的表格,我从网上找到了OpenFlashChart(of ...
- SQL Server(函数) 关键字的使用 三
三, 函数关键字 -- 使用介绍 28, Function的使用(Function的内建 SQL函数)? 内建 SQL 函数的语法是: SELECT function(列) FROM 表) 29, a ...