generate list of partially accumulated values

Vadim Radionov rvp74 at ua.fm
Sun Sep 16 12:42:37 EDT 2007


cesco пишет:
> Hi,
> 
> I have the following list:
> l = [1, 2, 3, 4]
> and I'd like to obtain a list like the following:
> l_partial_sum = [1, 3, 6, 10] (that is [1, 1+2, 1+2+3, 1+2+3+4])

 >>> def iscan(f, seq, acc=0):
...     for i in seq:
...        acc = f(acc,i)
...        yield acc
...
 >>> list(iscan(operator.add, xrange(1,5)))
[1, 3, 6, 10]
 >>>





More information about the Python-list mailing list