Home > Back-end >  Accumulate list elements while adding a constant to each accumulation
Accumulate list elements while adding a constant to each accumulation

Time:10-28

I'm accumulating values in a list so that a value is the sum of all previous values, using np.cumsum():

l=[4,2,1,3]
c = np.cumsum(l)
print(c)

[4 6 7 10]

But I'd like to also add 1 to every computation, so that the result looks like:

[4 7 9 13]

What's the simplest way to achieve that?

CodePudding user response:

Here you go:

res = c   np.arange(len(c))
  • Related