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函数 ...
随机推荐
- 阿里封神谈hadoop学习之路
阿里封神谈hadoop学习之路 封神 2016-04-14 16:03:51 浏览3283 评论3 发表于: 阿里云E-MapReduce >> 开源大数据周刊 hadoop 学生 s ...
- js代码 设为首页 加入收藏
// JavaScript Document // 加入收藏 <a onclick="AddFavorite(window.location,document.title)" ...
- Debug 之 The state information is invalid for this page and might be corrupted
1.问题描述: 网站部署之后,排序或者搜索之后报错:The state information is invalid for this page and might be corrupted 2.问题 ...
- Socket异步通信学习三
接下来是客户端部分,采用同步接收模式,在SocketClient项目中新建了一个SynServer类,用于存放socket服务器代码,和AsynServer类似,主要有4个方法: 有一个全局socke ...
- 获取mp4文件信息
计算电影长度 方法1 从mvhd - movie header atom中找到time scale和duration,duration除以time scale即是整部电影的长度. time scale ...
- oracle数据库创建用户,并且给用户授权
参考文档: http://www.blogjava.net/wolfman09/archive/2009/05/01/268536.html 一:创建用户 create user username i ...
- 修改BASH的配色
PS1变量简介 PS1是Linux终端用户的一个环境变量,用来说明命令行提示符的设置. \d :#代表日期,格式为weekday month date,例如:"Mon Aug 1" ...
- C#一些小知识点
1. 在Load时候由代码来做控件PictureBox,并且用代码将图片加载进去: private void Form2_Load(object sender, EventArgs e) { Dire ...
- override和new的区别【摘】
override 1. override是派生类用来重写基类中方法的: 2. override不能重写非虚方法和静态方法: 3. override只能重写用virtual.abstract.overr ...
- jquery数据验证插件(自制,简单,练手)
一:最近项目中js数据验证比较多,为了统一风格,移植复用,于是顺手封装了Jquery的插件. (function($) { var defaults = { bugColor: '#FFCCCC', ...