`

Python map、filter,reduce介绍

阅读更多
1、filter(function,iterable)
引用
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 iterable if item]if function is None.

demo:
#-*-coding:utf-8-*-
def foo():
    return filter(lambda x:x>5,range(0,10))
def bar():
    return [x for x in range(0,10) if x > 5]
print foo() == bar()

2、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. 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.

demo:
#-*-coding:utf-8-*-
def add(x,y):
    return x+y
print map(add, range(8),range(8))

3、reduce(function,iterable[,initalizer])
引用
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(lambdax, 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 theiterable. 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. Ifinitializer is not given and iterable contains only one item, the first item is returned. Roughly equivalent to:

demo:
#-*-coding:utf-8-*-
def foo():
    return reduce(lambda x,y:x*y,range(1,5))
print foo()


参考资料:
http://docs.python.org/2/library/functions.html
分享到:
评论

相关推荐

    简单了解python filter、map、reduce的区别

    这篇文章主要介绍了简单了解python filter、map、reduce的区别,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 python中有一些非常有趣的函数,面试的时候可能...

    详解python中三种高阶函数(map,reduce,filter).pdf

    详解python中三种高阶函数(map,reduce,filter)

    Python lambda表达式filter、map、reduce函数用法解析

    主要介绍了Python lambda表达式filter、map、reduce函数用法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

    Python内置函数之filter map reduce介绍

    Python内置了一些非常有趣、有用的函数,如:filter、map、reduce,都是对一个集合进行处理,filter很容易理解用于过滤,map用于映射,reduce用于归并. 是Python列表方法的三架马车。 1. filter函数的功能相当于...

    Python中的特殊语法:filter、map、reduce、lambda介绍

    主要介绍了Python中的特殊语法:filter、map、reduce、lambda介绍,本文分别对这个特殊语法给出了代码实例,需要的朋友可以参考下

    Python3的高阶函数map,reduce,filter的示例详解

    注意其中:map和filter返回一个惰性序列,可迭代对象,需要转化为list >>> a = 3.1415 >>> round(a,2) 3.14 >>> a_round = round >>> a_round(a,2) 3.14 >>> def func_devide(x, y, f): return f(x) - f(y) #传递...

    Python中sorted函数、filter类、map类、reduce函数

    文章目录sorted函数一、sort方法二、sorted内置函数三、情景引入filter类一、简单使用二、练习map类语法:一、简单使用二、练习reduce函数语法:一、简单使用二、设置初始值 Python中使用函数作为参数的内置函数和类...

    Python中map,reduce,filter和sorted函数的使用方法

    主要介绍了Python中map,reduce,filter和sorted函数的使用方法,是Python入门学习中的基础知识,需要的朋友可以参考下

    Pythont特殊语法filter,map,reduce,apply使用方法

    这篇文章主要介绍了Pythont特殊语法filter,map,reduce,apply使用方法,需要的朋友可以参考下 (1)lambda lambda是Python中一个很有用的语法,它允许你快速定义单行最小函数。类似于C语言中的宏,可以用在任何需要...

    Python-Parallel-Collections:支持并行mapreduce样式方法的Python集合

    如果您可以根据map / reduce / filter操作来定义问题,那么它将利用多核在计算机上的多个并行Python进程上运行。 请注意,尽管以下示例是以交互方式编写的,但由于多个过程的性质,它们可能实际上无法在交互解释器...

    Python中的map、reduce和filter浅析

    1、先看看什么是 iterable 对象 以内置的max函数为例子,查看其doc:复制代码 代码如下:>>> print max.__doc__max(iterable[, key=func]) -> valuemax(a, b, c, …[, key=func]) -> value With a single iterable ...

    python- 笔记 高阶函数map reduce fileter

    python 高阶函数:map reduce filter map函数:遍历序列,对序列中每个元素进行操作,最终获取新的序列。 reduce函数:对于序列内所有元素进行累计操作。 filter函数:对于序列中的元素进行筛选,最终获取符合条件的...

Global site tag (gtag.js) - Google Analytics