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. Nodejs mongodb 管理组件adminmongodb

    强大的 nodejs的mongodb管理工具,强大到即下即用: 安装需求: 1.git命令获取组件包,git clone https://github.com/mrvautin/adminMongo. ...

  2. 表字符集latin1,client如何查看中文字符

    表结构如下: . row *************************** Table: my_data_7e Create Table: CREATE TABLE `my_data_7e` ( ...

  3. 【机器学习】人工神经网络ANN

    神经网络是从生物领域自然的鬼斧神工中学习智慧的一种应用.人工神经网络(ANN)的发展经历的了几次高潮低谷,如今,随着数据爆发.硬件计算能力暴增.深度学习算法的优化,我们迎来了又一次的ANN雄起时代,以 ...

  4. linux 运维常用工具表

    https://code.google.com/p/httperf/  ※测量Web服务器的性能 ./configure   make &&make install http://ww ...

  5. prometheus client_golang使用

    序言 Prometheus是一个开源的监控系统,拥有许多Advanced Feature,他会定期用HTTP协议来pull所监控系统状态进行数据收集,在加上timestamp等数据组织成time se ...

  6. 开源巨献:Google最热门60款开源项目

    文章整理于互联网.本文收集了 60款 Google 开源的项目,排名顺序按照 Github ★Star 数量排列. 0.机器学习系统 TensorFlow  ★Star 62533 TensorFlo ...

  7. smartClient 1--框架介绍

    一.是什么(以下简称SC)     smartClient 是一个基于web技术的开发框架,主要包括: 一个无需安装的 Ajax/HTML5 客户端引擎 UI组件和服务(采用富客户端RIA)--- 提 ...

  8. Docker 三剑客之 Docker Swarm

    上一篇:Docker 三剑客之 Docker Compose 阅读目录: Docker Machine 创建 Docker 主机 Docker Swarm 配置集群节点 Docker Service ...

  9. C语言结构体1.1

    结构体组成 struct 结构体名: 类型名  成员名: 建立结构体 结构体名 类型名 { 成员: }: 建立一个关于学生信息的结构体(名字,年龄,性别,学号,成绩): 结构体定义 //结构体声明 s ...

  10. hdu 1496 Equations hash表

    hdu 1496 Equations hash表 题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1496 思路: hash表,将原来\(n^{4}\)降 ...