Re: Accumulate function in python



dhruvbird <dhruvbird@xxxxxxxxx> wrote:

On Jul 19, 4:28 pm, Peter Otten <__pete...@xxxxxx> wrote:
dhruvbird wrote:
  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.

Homework?

not really :)

It's just that I was wondering if a built-in function for doing such
things (which I find myself doing increasingly with an explicit loop)
exists.

Why would you find yourself doing it more than once? Write it once in a
function and then just re-use the code.
.



Relevant Pages

  • Re: Accumulate function in python
    ... And would like to compute the cumulative sum of all the integers ... from index zero into another array. ...   What is the best way to get this. ...
    (comp.lang.python)
  • Re: Accumulate function in python
    ... And would like to compute the cumulative sum of all the integers ... from index zero into another array. ...   What is the best way to get this. ...
    (comp.lang.python)
  • Re: "Sorting" assignment
    ... If the array is already sorted, this means that you end up ... less time than a bubble sort, ...     int intLeft, ... I don't declare anything inside of a loop is why. ...
    (comp.programming)
  • Re: #defining an array within another in C language
    ... you do not know the number of elements of the array. ...     int var; ... C99 flexible array members or the plain old "struct hack". ...
    (comp.lang.c)
  • Re: Generic programming in C
    ... struct tag$ArrayInterface; ...     unsigned int Flags; ... specific type as the contents of the array. ...
    (comp.lang.c)