0

I have a length 5 array, e.g.

array = [1,2,6,4,3]

What I want is a new array cumulatively summing a maximum set number of previous values, i.e. 3:

return_array = [1,3,9,12,13]

Using numpy's 'cumsum' function seemed like a good starting point, however it appears restricted to summing the complete range of elements, giving:

cumsum_array = np.cumsum(array)
cumsum_array = [1,3,9,13,16]

Am I missing something?

ds_chem
  • 173
  • 6

1 Answers1

-1

I would do this using zip:

a_ = [0] * 2 + a
list(map(sum, zip(a_, a_[1:], a_[2:])))

> [1, 3, 9, 12, 13]
MJeffryes
  • 458
  • 3
  • 14