itertools.accumulate. , :
def add2(x, y):
return (x + y,) * 2
:
mysteryFunction(range(5), add2, 0)
:
([0, 1, 3, 6, 10], 10)
0 4 .
itertools.accumulate , ( ) ; :
from itertools import accumulate
from operator import add
list(accumulate(range(5), add))
list mystery_function ( list), , list,
for partialsum in accumulate(range(5), add):
... do stuff with partialsum ...
, accumulate, , (, , , , , accumulate), ' d , , , .
, , accumulate. , base , base 1. ( base 10):
def addless(new, base):
return base + new, base - 1
mysteryFunction(range(5), addless, 10)
( range, base), ([10, 10, 10, 10, 10], 5). accumulate :
def addless2(last_base, new):
_, base = last_base
return base + new, base - 1
( , accumulate):
from itertools import accumulate, chain
base = 10
accum = accumulate(chain(((None, base),), range(5)), addless2)
next(accum)
out, (*_, base) = zip(*accum)
vals (10, 10, 10, 10, 10) base 5, ( ; zip , ).