Fastest way to calculate the sum of specific regions of an array

Viewed 7102

Given the following data (in python 2.7):

import numpy as np
a = np.array([1,2,3,4,5,6,7,8,9,10,11,12,14])
b = np.array([8,2,3])

I want to get the sum of the first 8 elements in a, then the sum of the 9 and 10 element and in the end the last 3 (basic the information in b). The desired output is:

[36, 19, 37]

I can do this with for loops and such, but there must be a more pythonic way and a more efficient way of doing!

5 Answers
Related