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函数 ...
随机推荐
- 动态追踪技术(中) - Dtrace、SystemTap、火焰图
http://openresty.org/cn/presentations.html http://weibo.com/agentzh?is_all=1 http://openresty.org/po ...
- page cache和buffer cache 图解
http://www.cnblogs.com/yrpen/p/3777963.html http://www.cnblogs.com/hustcat/archive/2011/10/27/222699 ...
- c++ primer,友元函数上的一个例子(By Sybase)
本文试图解释c++ primer Screen 和 Window_Mgr的例子,为什么将两个类放在两个文件中无法编译? 将两个类写在同一个文件中,通过三个例子解释问题: 第一种写法问题: 编译到Scr ...
- 文件I/O(不带缓冲)之open函数
调用open函数可以打开或创建一个文件. #include <fcntl.h> int open( const char *pathname, int oflag, ... /* mode ...
- 对于jdk jre jvm的简单认识
1:名词解释 jdk:java develop kit:java开发工具包 jre:java runtime environment :java开发运行时环境 jvm:java virtua m ...
- Vim 的补全模式加速器,轻松玩转全部 15 种自动补全模式
1. 关于 Vim 补全模式 ---- Vim 一共提供了 15 种自动补全的模式(:help ins-completion).其中有两种的补全列表内容与另外两种相同,只是排序不同,这 15 种 ...
- bootstrap兼容IE
这种响应式的布局正是通过CSS3的媒体查询(Media Query)功能实现的,根据不同的分辨率来匹配不同的样式.IE8浏览器并不支持这一优秀的Css3特性,Bootstrap在开发文档中写了如何使用 ...
- Centos 6.4 安装elasticsearch+kibana
elasticsearch和kibanna的链接地址:https://www.elastic.co/downloads,我的环境里用的包为kibana-4.1.1-linux-x64.tar.gz和e ...
- ADO.NET通用数据库访问类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- (转载)研究openvswitch的流量模式
最近又开始弄openvswitch,网上也有不少的资料,但是发觉都集中在openvswitch安装及简单使用或者一些原码分析,从心里来讲,感觉这些和心里得到的差距有点大啊,其实更希望能类似资料在ope ...