def my_sequences(x):
    x = x[x != 0]
y = np.cumsum(x, dtype=np.intp)
a = np.ones(y[-1], dtype=np.intp)
a[y[:-1]] -= x[:-1]
return np.cumsum(a, out=a)
Efficient way to create an array that is a sequence of variable length ranges in numpy