Here's a compilation of the suggestions (answers and comments) and their timings:
import numpy as np c = np.array([np.random.rand(np.random.randint(1, 300)) for i in range(50)]) def oliver(arr): res = np.empty_like(arr) for enu, subarr in enumerate(arr): res[enu] = np.sum(subarr) return res def reut(arr): return np.array([a.sum() for a in arr]) def hpaulj(arr): d = np.concatenate(arr) l = map(len, arr) i = np.cumsum(l) - l return np.add.reduceat(d, i)
And their times:
In [94]: timeit oliver(c) 1000 loops, best of 3: 457 µs per loop In [95]: timeit reut(c) 1000 loops, best of 3: 317 µs per loop In [96]: timeit hpaulj(c) 10000 loops, best of 3: 94.4 µs per loop
It was somewhat tricky to implement @hpaulj's, but I think I got it (and it's the fastest if you use concatenate instead of hstack)
sums = [sum(arr) for arr in [a, b]]?a,b, etc longer or shorter thanc, typically? Iscalready created, or can we bypass that step?aandbcan vary between length 1 and a few 100.ctypically has length of a few tens andcis already created.np.add.reduceat(d,ind)is a fast way of summing uneven blocks of the arrayd. But constructingd(np.hstack(c)) andindtakes time.