Accumulate function in python

Nitin Pawar nitinpawar432 at gmail.com
Mon Jul 19 07:25:47 EDT 2010


Hi,

you may want to do like this

array=[0,1,2]
sumArray = []

for element in range(0,len(array)):
    if element == 0 :
        sumArray.append(array[element])
    else:
        sumArray.append((array[element] + sumArray[element-1]))

and then you can recheck it

Thanks,
nitin

On Mon, Jul 19, 2010 at 4:48 PM, dhruvbird <dhruvbird at gmail.com> wrote:

> Hello,
>  I have a list of integers: x = [ 0, 1, 2, 1, 1, 0, 0, 2, 3 ]
>  And would like to compute the cumulative sum of all the integers
> from index zero into another array. So for the array above, I should
> get: [ 0, 1, 3, 4, 5, 5, 5, 7, 10 ]
>  What is the best way (or pythonic way) to get this.
>
> Regards,
> -Dhruv.
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
Nitin Pawar
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100719/1ad34c57/attachment-0001.html>


More information about the Python-list mailing list