itertools.accumulate would work the same as numpy.cumsum:
from operator import add
from itertools import accumulate
from operator import add
def cum_sum(l):
return accumulate(l, add)
In [22]: list(cum_sum(my_array))
Out[22]: [1, 5, 6, 19, 28]
which will match cumsum exactly.
If you want to ignore the last element:
from operator import add
from itertools import islice, accumulate
def cum_sum(l, take):
return accumulate(islice(my_array, 0, len(l)-take), add)
In [16]: list(cum_sum(my_array, 1))
Out[16]: [1, 5, 6, 19]
To exactly match your output including the 0 and to work in python2 or 3 you can create a generator function:
my_array = [1, 4, 1, 13, 9]
def cum_sum(l):
sm = 0
for ele in l:
yield sm
sm += ele
Output:
In [5]: my_array = [1, 4, 1, 13, 9]
In [6]: list(cum_sum(my_array))
Out[6]: [0, 1, 5, 6, 19]
np.cumsum