From: dhruvbird on
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.
From: Peter Otten on
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?

>>> def cumulative_sum(values, start=0):
.... for v in values:
.... start += v
.... yield start
....
>>> list(cumulative_sum([ 0, 1, 2, 1, 1, 0, 0, 2, 3 ]))
[0, 1, 3, 4, 5, 5, 5, 7, 10]

Peter
From: Vlastimil Brom on
2010/7/19 dhruvbird <dhruvbird(a)gmail.com>:
> 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.
> --

Hi,
just a straightworward, naive approach...:

lst_int = [ 0, 1, 2, 1, 1, 0, 0, 2, 3 ]
acc_int = 0
output_lst = []
for i in lst_int:
acc_int += i
output_lst.append(acc_int)
print output_lst

vbr
From: Mick Krippendorf on
Am 19.07.2010 13:18, dhruvbird 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.

<python>

import copy
import itertools

def acc(items, copy=copy.deepcopy):
items = iter(items)
result = next(items)
yield copy(result)
for item in items:
result += item
yield copy(result)


print list(acc([0, 1, 2, 1, 1, 0, 0, 2, 3]))
print list(itertools.islice(acc(itertools.count()), 10))
print list(acc(['a', 'b', 'c']))
print list(acc([[a], [b], [c]]))

</python>

Output:

[0, 1, 3, 4, 5, 5, 5, 7, 10]
[0, 1, 3, 6, 10, 15, 21, 28, 36, 45]
['a', 'ab', 'abc']
[[a], [a, b], [a, b, c]]


Without copy.deepcopy() the last line would be:

[[a, b, c], [a, b, c], [a, b, c]]


The copy=copy.deepcopy parameter allows for things like this:

>>> print list(acc([[a], [b], [c]], tuple))
[(a,), (a, b), (a, b, c)]


or:

>>> print list(acc([['a'], ['b'], ['f'], ['s'], ['c'], ['g']], max))
['a', 'b', 'f', 's', 's', 's']


or:

>>> data = [[0], [1], [2], [1], [1], [2], [3]]
>>> print list(acc(data, lambda x: float(sum(x)) / float(len(x))))
[0.0, 0.5, 1.0, 1.0, 1.0, 1.1666666666666667, 1.4285714285714286]


Endless possibilities in an endless universe.


Regards,
Mick.
From: Andre Alexander Bell on
On 07/19/2010 01:18 PM, dhruvbird 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.

Maybe not pythonic, but straight-forward:

>>> import numpy
>>> numpy.cumsum(x)
array([ 0, 1, 3, 4, 5, 5, 5, 7, 10])

An example with a class

class CumulativeSum(object):
def __init__(self, start=0):
self._current = start
def __call__(self, value):
self._current += value
return self._current

>>> cummulative_sum = CumulativeSum(0)
>>> map(cummulative_sum, x)
[0, 1, 3, 4, 5, 5, 5, 7, 10]

Dirty:

current = 0

def cummulative_sum(value):
global current
current += value
return current

>>> map(cummulative_sum, x)
[0, 1, 3, 4, 5, 5, 5, 7, 10]

Weird:

def cummulative_sum_reducer(x, y):
x.append(x[-1] + y)
return x

>>> reduce(cummulative_sum_reducer, x, [0])
[0, 0, 1, 3, 4, 5, 5, 5, 7, 10]

Cheers


Andre