I have an array:
my_array = [1, 4, 1, 13, 9]
and would like to create a new array that for each index in my_array is the sum of all the previous index values
summed_array = [0, 1, 5, 6, 19]
I tried something like
for ind,i in enumerate(my_array):
print i, my_array[ind-1]
but can't figure out how to get the summation over all previous values.
>>> from numpy import cumsum, ones
>>> a = ones(10)
>>> print(cumsum(a))
array([ 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.])
A pure Python implementation:
def cumilative_sum(lst):
total, result = 0, []
for ele in lst:
result.append(total)
total += ele
return result
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With