def?calc_prod(lst):
????def?prod():
????????return?reduce(lambda?x,?y?:?x?*?y,?lst)
????return?prod
f?=?calc_prod([1,?2,?3,?4])
print?f()
1 回答
已采納

清波
TA貢獻165條經驗 獲得超90個贊
官方解釋如下:
? ? reduce(...)
? ? ? ? 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.
大致意思是,放入兩個參數(shù),一個是function, 另外一個是 可迭代對象,然后會返回用function 代入 可迭代對象后 的值。 類似官方解釋中的案例:
reduce(lambda?x,?y:?x+y,?[1,?2,?3,?4,?5])? ##?結果為?:?((((1+2)+3)+4)+5). 即:?sum([1,2,3,4,5]),?列表各項的和
添加回答
舉報
0/150
提交
取消