Encomium
Encomium

Reputation: 277

Accumulate list elements while adding a constant to each accumulation

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?

Upvotes: 0

Views: 41

Answers (1)

sehan2
sehan2

Reputation: 1835

Here you go:

res = c + np.arange(len(c))

Upvotes: 1

Related Questions