python - Sum slices of consecutive values in a NumPy array -


let's have numpy array a containing 10 values. example situation here, although repeat same array length 100.

a = np.array([1,2,3,4,5,6,7,8,9,10]) 

i sum first 5 values followed second 5 values , on , store them in new empty list b.

so b contain b = [15,40].

how go doing it?

try list comprehension:

b = [sum(a[current: current+5]) current in xrange(0, len(a), 5)] 

it takes slices of 5 @ time list, sums them , constructs list. works lists aren't multiple of 5 in length.

(xrange should range in python3+)


Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -