为什么这里的reduce 函数需要加上第三个参数?
def calc_prod(lst):
def lazy_prod():
def f(x,y):
return x*y
return reduce(f,1st,1)
#return reduce(f,1st)
return lazy_prod
f = calc_prod([1, 2, 3, 4])
print f()
def calc_prod(lst):
def lazy_prod():
def f(x,y):
return x*y
return reduce(f,1st,1)
#return reduce(f,1st)
return lazy_prod
f = calc_prod([1, 2, 3, 4])
print f()
2015-04-07
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.
自己解读下这句话,应该对你有帮助 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.
举报