functools下的reduce函数

来看下 reduce 的源码:

################################################################################
### reduce() sequence to a single item
################################################################################
_initial_missing = object()

def reduce(function, sequence, initial=_initial_missing):
    """
    reduce(function, sequence[, initial]) -> value

    Apply a function of two arguments cumulatively to the items of a sequence,
    from left to right, so as to reduce the sequence to a single value.
    For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates
    ((((1+2)+3)+4)+5).  If initial is present, it is placed before the items
    of the sequence in the calculation, and serves as a default when the
    sequence is empty.
    """

    it = iter(sequence)        # <----------- 将其变为一个迭代器

    if initial is _initial_missing:
        try:
            value = next(it)   # <----------- 取得 it 的第一个元素
        except StopIteration:
            raise TypeError("reduce() of empty sequence with no initial value") from None
    else:
        value = initial        # <----------- 这一步一般没啥用,除非你手动指定最初元素

    for element in it:
        value = function(value, element) # 如果 value = next(it) 则从第二个元素开始
		# 如果 value = initial 则 for 从 it 的第一个元素开始
		
    return value

看下上边的注释,就更清楚了:

从左到右,将序列元素两两规约,直到为一个元素
举个例子:

reduce(lambda x, y: x+y, [1, 2, 3, 4, 5])

最后返回的是这个结果:

((((1+2)+3)+4)+5)

如果 initial 传入参数,则在计算中将其放置在序列的项目之前,并在序列为空时用作默认值

再来看个例子:

在PaddleSeg Transform Normalize 的 __init__ 中,有这么三行:

from functools import reduce
if reduce(lambda x, y: x * y, self.std) == 0:
	raise ValueError('{}: std is invalid!'.format(self))

其实就是将 self.std 的三个元素相乘,如果有一个元素为0,则 raise ValueError

(因为 self.std 要做分母)

然而实际上是 从 _functools 中导入的,并没有用上边的Python代码

try:
    from _functools import reduce
except ImportError:
    pass

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值