filter(function, iterable)
Construct a list from those elements of iterable for which function returns true.
  对iterable中的item依次执行function(item),将执行结果为True的item组成一个List/String/Tuple(取决于iterable的类型)返回. iterable包括列表,iterator等。一个简单例子,过滤出一个整数列表中所有的奇数
>>> lst = [1,2,3,4,5,6,7]
>>> filter(lambda e: e % 2, lst)
[1, 3, 5, 7]
 
filter也有一个返回迭代器的版本:itertools.ifilter
 
filter完全可以用list comprehension实现 : [elem for elem in iterable if function(elem)]
 
map(function, iterable,...) :

Apply function to every item of iterable and return a list of the results. If additional iterable arguments are passed, function must take that many arguments and is applied to the items from all iterables in parallel.

  如果只有一个iterable,那么对iterable中的item依次执行function(item),见执行结果组成一个List返回。如果有多个iterable, 那么function需要同时接受多个参数。
  第一个例子将所有元素乘以2
>>> lst = [1,2,3,4,5,6,7]
>>> map(lambda e: e * 2, lst)
[2, 4, 6, 8, 10, 12, 14]
 
   第二个例子将两个序列的对应元素相加
>>> lst = [1,2,3,4,5,6,7]
>>> map(lambda e1, e2: e1 + e2, lst, (7, 6, 5, 4, 3, 2, 1))
[8, 8, 8, 8, 8, 8, 8]
 
 
map也有一个返回迭代器的版本:itertools.imap
 
对于只有一个iterable的版本 等价于 [function(item) for item in iterable]
 
对于多个iterable的版本 基本等价于[function(*items) for item in zip(*iterable)], 比如上面第二个例子等同于 [x + y for (x, y) in zip(lst0, lst1)]
但是对于多个序列长度不一致的情况,zip和map的处理是不一样的,zip以最短长度为准;map以最长长度为准,较短的序列用None填充
>>> zip((1,2,3), ('a', 'b'))
[(1, 'a'), (2, 'b')]
 
>>> map(lambda x, y:(x, y), (1,2,3), ('a', 'b'))
[(1, 'a'), (2, 'b'), (3, None)]
>>>
 
reduce(function, iterable, init_value)
Apply function of two arguments cumulatively to the items of iterable, from left to right, so as to reduce the iterable to a single value
  对iterable中的item顺序迭代调用function,如果有inti_value,还可以作为初始值调用。function接受两个参数,第一个参数,历史迭代值,第二参数,新的迭代元素。举个简单例子,对序列所有元素的平方求和:
>>> reduce(lambda x, y: x + y *y, (1, 2, 3), 0)
14
 
如果sequence中只有一个元素 且没有inti_value,那么会返回第一个元素
    
 
  笔者之前有一个简单需求:把多个list整合到一个list。for example:  [[1,2,3], [5], [5, 6]]  ===》》》 [1, 2, 3, 5, 5, 6]
  当时试过用filter map reduce来实现, 当然实现都不pythonic,直到后来发现了itertools.chain
 # -*- coding: utf-8 -*-
def test():
lst = [[1,2,3], [5], [5, 6]]
print ' ---- use filter ----'
ret = []
print filter(lambda e: ret.extend(e), lst)
print ret print ' ---- use map ----'
ret = []
print map(lambda e: ret.extend(e), lst)
print ret print ' ---- use reduce ----'
ret = reduce(lambda r, e: r.extend(e) or r, lst, [])
print ret print ' ---- use itertools.chain ----'
import itertools
print list(itertools.chain(*lst)) if __name__ == '__main__':
test()

最后,只要可以,尽量使用list comprehension

python filter map reduce的更多相关文章

  1. python: filter, map, reduce, lambda

    filter built-in function filter(f,sequence) filter can apply the function f to each element of seque ...

  2. Python学习(五)函数 —— 内置函数 lambda filter map reduce

    Python 内置函数 lambda.filter.map.reduce Python 内置了一些比较特殊且实用的函数,使用这些能使你的代码简洁而易读. 下面对 Python 的 lambda.fil ...

  3. Python之匿名函数(filter,map,reduce)

    参考博客:Python匿名函数详解--http://blog.csdn.net/csdnstudent/article/details/40112803 Python内建函数之——filter,map ...

  4. Python: lambda, map, reduce, filter

    在学习python的过程中,lambda的语法时常会使人感到困惑,lambda是什么,为什么要使用lambda,是不是必须使用lambda? 下面就上面的问题进行一下解答. 1.lambda是什么? ...

  5. Python经常使用内置函数介绍【filter,map,reduce,apply,zip】

    Python是一门非常简洁,非常优雅的语言,其非常多内置函数结合起来使用,能够使用非常少的代码来实现非常多复杂的功能,假设相同的功能要让C/C++/Java来实现的话,可能会头大,事实上Python是 ...

  6. Python内置函数之filter map reduce

    Python内置函数之filter map reduce 2013-06-04 Posted by yeho Python内置了一些非常有趣.有用的函数,如:filter.map.reduce,都是对 ...

  7. filter,map,reduce,lambda(python3)

    1.filter filter(function,sequence) 对sequence中的item依次执行function(item),将执行的结果为True(符合函数判断)的item组成一个lis ...

  8. Python2.7学习笔记-定义函数、filter/map/reduce/lambda

    我把写的代码直接贴在下面了,注释的不是很仔细,主要是为了自己复习时方便查找,并不适合没有接触过python的人看,其实我也是初学者. #定义函数 def my_abs(x): if x>=0: ...

  9. python基础——map/reduce

    python基础——map/reduce Python内建了map()和reduce()函数. 如果你读过Google的那篇大名鼎鼎的论文“MapReduce: Simplified Data Pro ...

随机推荐

  1. bootstrap-wysihtml5设置值

    今天做项目的时候用到了 bootstrap-wysihtml5,添加完一篇文章返回去继续添加的时候,js已经设置了将所有变量的值清空,但是不管用$('#someId').val(")还是$( ...

  2. Linux端图形处理工具ImageMagick在Centos上的安装

    一.安装背景程序要用到用户上传图片,编辑的功能,能进行旋转,裁剪,缩放等. 二.ImageMagick介绍 ImageMagick是用C语言开发图片处理程序.可以对图片进行改变大小.旋转.锐化.减色或 ...

  3. MES制造执行系统启动篇

    美国先进制造研究机构(AMR)定义了MES为:"位于上层的计划管理系统与底层的工业控制之间的面向车间层的管理信息系统",它为操作人员/管理人员提供计划的执行.跟踪以及所有资源(人. ...

  4. tensorflow 从入门到上天教程一

    tensorflow 是一个google开源的深度学习的框架,执行性能良好,值得使用. caffe,caffe2 通过配置就可以拼凑一个深度学习框架,大大简化流程但也依赖大量的开源库,性能也不错.20 ...

  5. Eclipse的几个常用快捷键

    键盘操作 功能 Alt + /    语句或变量名自动补全 Ctrl + Shift + F 语句格式化 Ctrl + / 单行注释(或取消单行注释) Ctrl + Shift + /   多行注释 ...

  6. C#参考之sealed密封类(转)

    C# 语言参考 sealed(C# 参考) 当对一个类应用 sealed 修饰符时,此修饰符会阻止其他类从该类继承.在下面的示例中,类 B 从类 A 继承,但是任何类都不能从类 B 继承. 1     ...

  7. YII 多表联查 纵表

    A id 与B a.id B id 与C b.id C id 与D c.id 查A读D数据 应用场景: order表 ordergoods表 goods表 merchant加盟商 order 与ord ...

  8. (一)windows7下solr7.1.0默认jetty服务器环境搭建

    windows7下solr7.1.0默认jetty服务器环境搭建 1.下载solr solr7官网地址:http://lucene.apache.org/solr/ jdk8官网地址:http://w ...

  9. linux下expect命令实现批量ssh免密

    有时候我们需要批量发送ssh命令给服务器,但是有可能有些服务器是新加入的,还没有配置ssh免密,这个时候就会提示我们输入yes/no 或者password等,expect脚本命令就是用于在提示这些的时 ...

  10. Zabbix 3.0 部署监控 [二]

    一.添加监控主机及设置   1.创建主机 Agent可以干一些SNMP无法干的事情,例如自定义监控项 snmp相关文章:http://www.abcdocker.com/abcdocker/1376  ...