使用 reduce

def multiply(s1, s2):
    print('{arg1} * {arg2} = {res}'.format(arg1=s1, 
                                           arg2=s2, 
                                           res=s1*s2))
    return s1 * s2

asequence = [1, 2, 3]

给定 initializer,通过将函数应用于初始化器和第一个可迭代元素来启动该函数:

cumprod = reduce(multiply, asequence, 5)
# Out: 5 * 1 = 5
#      5 * 2 = 10
#      10 * 3 = 30
print(cumprod)
# Out: 30

没有 initializer 参数,reduce 首先将函数应用于前两个列表元素:

cumprod = reduce(multiply, asequence)
# Out: 1 * 2 = 2
#      2 * 3 = 6
print(cumprod)
# Out: 6