def multirange_loop(counts):
    counts = np.asarray(counts)
ranges = np.empty(counts.sum(), dtype=int)
seq = np.arange(counts.max())
starts = np.zeros(len(counts), dtype=int)
starts[1:] = np.cumsum(counts[:-1])
for start, count in zip(starts, counts):
    ranges[start:start + count] = seq[:count]
return ranges
How to efficiently concatenate many arange calls in numpy?