lambda(),map(),reduce(),filter()

lambda()

官方文档:
An anonymous inline function consisting of a single expression which is evaluated when the function is called. The syntax to create a lambda function is

lambda [arguments]: expression

冒号前面为参数,冒号后面为返回值
其中参数是可选的,同时也支持默认值和变长参数

    >>> map(lambda x:x**2,range(1,10))
    [1, 4, 9, 16, 25, 36, 49, 64, 81]

    >>> func = lambda :True
    >>> func()

    >>> map(lambda x,y=2:x+y,range(1,10))
    [3, 4, 5, 6, 7, 8, 9, 10, 11]

    >>> l = lambda *args:args
    >>> l(1,2,3)
    (1, 2, 3)

    ##expression里面支持条件判断
    >>> map(lambda x:True if x % 2 == 0 else False ,range(1,11))
    [False, True, False, True, False, True, False, True, False, True]

map()

官方文档:
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 withNoneitems. If function isNone, 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.

  1. Apply function to every item of iterable and return a list of the results.
    将一个可迭代的对象一次映射到一个列表(注:python3中是映射到另一个可迭代对象。可以使用list()工厂函数装换为list)

    >>> map(lambda x:x**2,range(1,10))
    [1, 4, 9, 16, 25, 36, 49, 64, 81]
  2. 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 withNoneitems.
    支持传递多个可迭代对象作为参数,同时个数与函数参数的个数相同。若其中一个可迭代对象较短,则会自动补齐为none

    >>> map(lambda x,y:x+y,range(1,4),range(1,4))
    [2, 4, 6]
  3. If function isNone, the identity function is assumed.
    函数为None的话,会被定义为一个identity函数(类似于zip())

    >>> map(None,range(1,3),range(1,4))
    [(1, 1), (2, 2), (None, 3)]

reduce()

文档:
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.syntax:

reduce(function, iterable[, initializer])

对iterable里面的iterms从左到右迭代调用function函数。得到一个迭代值。initializer若存在,则作为初始值调用。
注:funcuion是一个有两个参数的函数

#类似于:
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(lambda x,y:x+y,range(1,11))
55
>>> reduce(lambda x,y:x+y,range(1,11),10)
65

filter()

文档:
Construct a list from those elements of iterable for which function returns true.
syntax:

filter(function, iterable)

用func依次作用于iterable,对于返回True的元素重新组成一个新的列表。
注:

  • 如果iterable是字符串,或者是元组,则返回的也是字符串或者元组。对于其他可迭代对象,返回的是列表。
  • 如果函数是None的话,则会直接判断iterable的elements是否为真。

    >>> filter(None,(0,12,34,15,1,3,0,3))
    (12, 34, 15, 1, 3, 3)
    >>> filter(None,range(0,11))
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

    最后:
    See itertools.ifilter() and itertools.ifilterfalse() for iterator versions of this function, including a variation that filters for elements where the function returns false.
    可以通过itertools.ifilter()和itertools.ifilterfalse()这个函数的迭代版本,对其中函数返回false的元素筛选。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值