1 回答

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