Cumulative Sum List
<p>I'm trying to create a cumulative list, but my output clearly isn't cumulative. Does anyone know what I'm doing wrong?
Thanks</p>

<pre><code>import numpy as np
import math
import random

l=[]
for i in range (50):
    def nextTime(rateParameter):
        return -math.log(1.0 - random.random()) / rateParameter
    a = np.round(nextTime(1/15),0)
    l.append(a)
np.cumsum(l)
print(l)
</code></pre>

<p>If you are using <code>numpy</code> then <code>numpy</code> is the way to go, however, if all you need to do is provide a cumulative sum then Python3 comes with <code>itertools.accumulate</code>:</p>

<pre><code>def nextTime(rateParameter):
    while True:
        yield -math.log(1.0 - random.random()) / rateParameter

&gt;&gt;&gt; list(it.accumulate(map(round, it.islice(nextTime(1/15), 50))))
[2, 9, 14, 26, 27, ...]
</code></pre>

python numpy cumsum