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!
CodePudding user response:
You can use np.cumsum
:
import numpy as np
a = np.array([1, 2, 3])
print(np.cumsum(a[::-1])[::-1])
# Outputs [6 5 3]
CodePudding user response:
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)