参考python built-on function:

http://docs.python.org/2.7/library/functions.html?highlight=map%20reduce

map(functioniterable...)

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. If one iterable is shorter than another it is assumed to be extended with None items. If function is None, the identity function is assumed; if there are multiple arguments, map() returns a list consisting of tuples containing the corresponding items from all iterables (a kind of transpose operation). The iterable arguments may be a sequence or any iterable object; the result is always a list.

>>> def cube(x): return x*x*x 
>>> map(cube, range(1, 11)) 
[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]

另外map也支持多个sequence,这就要求function也支持相应数量的参数输入:
>>> def add(x, y): return x+y 
>>> map(add, range(8), range(8)) 
[0, 2, 4, 6, 8, 10, 12, 14]

reduce(functioniterable[, initializer])

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. For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates ((((1+2)+3)+4)+5). The left argument, x, is the accumulated value and the right argument, y, is the update value from the iterable. If the optional initializer is present, it is placed before the items of the iterable in the calculation, and serves as a default when the iterable is empty. If initializer is not given and iterablecontains only one item, the first item is returned. Roughly equivalent to:

def reduce(function, iterable, initializer=None):
it = iter(iterable)
if initializer is None:
try:
initializer = next(it)
except StopIteration:
raise TypeError('reduce() of empty sequence with no initial value')
accum_value = initializer
for x in it:
accum_value = function(accum_value, x)
return accum_value

reduce(function, sequence, starting_value):对sequence中的item顺序迭代调用function,如果有starting_value,还可以作为初始值调用,例如可以用来对List求和:
>>> def add(x,y): return x + y 
>>> reduce(add, range(1, 11)) 
55 (注:1+2+3+4+5+6+7+8+9+10)
>>> reduce(add, range(1, 11), 20) 
75 (注:1+2+3+4+5+6+7+8+9+10+20)

又例如:

a = [1,2,3,4,5]

reduce(lambda x, y : x +y, a)

则返回值为:15

加入我们设定init的值,即:

a = [1,2,3,4,5]

reduce(lambda x, y : x +y, a, 3)

则返回值为:18

注意:传递给reduce的func,必须要能接受两个参数。并返回一个结果。这个结果将会是传递给func进行下次一调用;上面的例子,调用过程是:

1,func(1, 2) 返回 x1

2,func(x1, 3) 返回 x2

3,func(x2, 4) 返回 x3

…...

其实,通过,reduce中func的设计,reduce可以完成map和filter的功能。

lambda:这是Python支持一种有趣的语法,它允许你快速定义单行的最小函数,类似与C语言中的宏,这些叫做lambda的函数,是从LISP借用来的,可以用在任何需要函数的地方: 
>>> g = lambda x: x * 2 
>>> g(3) 

>>> (lambda x: x * 2)(3) 
6

filter(functioniterable)

Construct a list from those elements of iterable for which function returns true. iterable may be either a sequence, a container which supports iteration, or an iterator. If iterable is a string or a tuple, the result also has that type; otherwise it is always a list. If function is None, the identity function is assumed, that is, all elements of iterable that are false are removed.

Note that filter(function, iterable) is equivalent to [item for item in iterable if function(item)] if function is not None and [item for item in iterableif item] if function is None.

See itertools.ifilter() and itertools.ifilterfalse() for iterator versions of this function, including a variation that filters for elements where the function returns false.

filter(function, sequence):对sequence中的item依次执行function(item),将执行结果为True的item组成一个List/String/Tuple(取决于sequence的类型)返回:
>>> def f(x): return x % 2 != 0 and x % 3 != 0 
>>> filter(f, range(2, 25)) 
[5, 7, 11, 13, 17, 19, 23]

>>> [i for i in range(2,25) if f(i)] 也输出:
[5, 7, 11, 13, 17, 19, 23]

>>> def f(x): return x != 'a' 
>>> filter(f, "abcdef") 
'bcdef'

我们也可以把filter map reduce 和lambda结合起来用,函数就可以简单的写成一行。
例如
kmpathes = filter(lambda kmpath: kmpath,                  
map(lambda kmpath: string.strip(kmpath),
string.split(l, ':')))              
看起来麻烦,其实就像用语言来描述问题一样,非常优雅。
对 l 中的所有元素以':'做分割,得出一个列表。对这个列表的每一个元素做字符串strip,形成一个列表。对这个列表的每一个元素做直接返回操作(这个地方可以加上过滤条件限制),最终获得一个字符串被':'分割的列表,列表中的每一个字符串都做了strip,并可以对特殊字符串过滤。

一句话总结:

filter很容易理解用于过滤,map用于映射,reduce用于归并。

lambda表达式返回一个函数对象
例子:
func = lambda x,y:x+y
func相当于下面这个函数
def func(x,y):
    return x+y
 
注意def是语句而lambda是表达式
下面这种情况下就只能用lambda而不能用def
[(lambda x:x*x)(x) for x in range(1,11)]
 [表达式 for 变量 in 列表 if 条件]
map,reduce,filter中的function都可以用lambda表达式来生成!

python map, reduce,filter 使用的更多相关文章

  1. Python map/reduce/filter/sorted函数以及匿名函数

    1. map() 函数的功能: map(f, [x1,x2,x3]) = [f(x1), f(x2), f(x3)] def f(x): return x*x a = map(f, [1, 2, 3, ...

  2. Python map,reduce,filter,apply

    map(function, iterable, ...) map()函数接收两个参数,一个是函数,一个是可迭代的对象,map将传入的函数依次作用到序列的每个元素,并把结果作为新的list返回. 基本等 ...

  3. [python基础知识]python内置函数map/reduce/filter

    python内置函数map/reduce/filter 这三个函数用的顺手了,很cool. filter()函数:filter函数相当于过滤,调用一个bool_func(只返回bool类型数据的方法) ...

  4. Demo of Python "Map Reduce Filter"

    Here I share with you a demo for python map, reduce and filter functional programming thatowned by m ...

  5. Python学习:函数式编程(lambda, map() ,reduce() ,filter())

    1. lambda: Python 支持用lambda对简单的功能定义“行内函数” 2.map() : 3.reduce() : 4.filter() : map() ,reduce() , filt ...

  6. python 函数式编程之lambda( ), map( ), reduce( ), filter( )

    lambda( ), map( ), reduce( ), filter( ) 1. lambda( )主要用于“行内函数”: f = lambda x : x + 2 #定义函数f(x)=x+2 g ...

  7. python--函数式编程 (高阶函数(map , reduce ,filter,sorted),匿名函数(lambda))

    1.1函数式编程 面向过程编程:我们通过把大段代码拆成函数,通过一层一层的函数,可以把复杂的任务分解成简单的任务,这种一步一步的分解可以称之为面向过程的程序设计.函数就是面向过程的程序设计的基本单元. ...

  8. map/reduce/filter/lambda

    Python内建了map()/reduce()/filter()函数. map()函数接收两个参数,一个是函数,一个是Iterable,map将传入的函数依次作用到序列的每个元素,并把结果作为新的It ...

  9. Python-函数式编程-map reduce filter lambda 三元表达式 闭包

    lambda 匿名函数,核心是作为算子,处理逻辑只有一行但具有函数的特性,核心用于函数式编程中 三元运算符 其实本质上是if分支的简化版,满足条件返回 if 前面的值,不满足条件返回 else后面的值 ...

随机推荐

  1. 踩坑学php(1)

    前言: 为什么要学php 呢?作为一个前端,一直有着了解后台的好奇心:作为一个计算机毕业的,一直有着实践更多设计模式和数据库相关的东西:而php 非常流行,拥有非常多的资源,入门应该容易: 为什么叫& ...

  2. Python学习笔记(五)Python的切片和迭代

    切片 Python提供了切片操作符,可以对list.tuple.字符串进行截取操作. list中的切片应用 语法如下: >>> L = ['Michael', 'Sarah', 'T ...

  3. A/B的困扰

    在学会简单A+B后,在实验A/B时遇到了下面的问题. #include<stdio.h> #include<stdlib.h> int main() { int a,b; sc ...

  4. fzu 1753 Another Easy Problem

    本题题意为求 t (t<150) 个 c (n,m)  (1<=m<=n<=100000)的最大公因子: 本题的难点为优化.主要有两个优化重点.一是每次对单个素因子进行处理,优 ...

  5. 32位程序调用Oracle11gR2数据库libclntsh.so失败

    [问题描述]32位程序调用Oracle11gR2数据库的libclntsh.so库时会返回失败. [问题原因]32位程序只能调用32位的Oracle客户端实例包,而R2数据库默认安装完毕后是没有lib ...

  6. QTDesigner的QVBoxLayout自动随窗口拉伸

    在MainWindow的构造函数中添加如下代码://设置Uiui.setupUi(this); //使Ui可自适应父窗口大小QVBoxLayout* mainLayout = new QVBoxLay ...

  7. Delphi HTML5 Canvas组件

    最近去sourceforge瞎转悠,突然发了一个组件,关于Delphi下Html5的canvas的组件,大概浏览了一下源码,竟然是纯粹的Pascal代码,也就说完全的Delphi代码.不敢独享,现在上 ...

  8. asp.net基础学习笔记

    原文地址:http://blog.csdn.net/oxoxzhu/article/details/8652530 1.概论 浏览器-服务器 B/S 浏览的      浏览器和服务器之间的交互,形成上 ...

  9. linux服务器开发浅谈

    [开发前准备] 在进行linux服务器开发之前,必须很清楚地了解所开发的对象需要考虑的相关问题比如:功能架构:提供服务的模块体系结构稳定性:服务器的出core率,内存泄露情况性能:请求与返回的速度与正 ...

  10. 使用xmanager 远程redhat6.3

    之前装过一次,特别麻烦,装上只有远程还卡卡的,这次按照教程居然装的灰常顺利,不符合我bug体质的特性,一定要记下来啊~~~ 1.先关闭防火墙 # service iptables stop #chkc ...