If I have an array [a,b,c], how can I convert it to [a+b+c, b+c, c] in the most efficient way?
(a, b, c) are floats.
Thanks!
If I have an array [a,b,c], how can I convert it to [a+b+c, b+c, c] in the most efficient way?
(a, b, c) are floats.
Thanks!
Use numpy.cumsum()
on the reversed array, then reverse it again. cumsum([a,b,c])
returns [a,a+b,a+b+c]
.
import numpy as np
x = [3,7,14]
y = np.cumsum(x[::-1])[::-1]
print(y)