Say you have 1d numpy array:
[0,0,0,0,0,1,2,3,0,0,0,0,4,5,0,0,0]
How would you create the following groups without using for loop?
[1,2,3], [4,5]
Here's one way using np.split:
a
# array([0, 0, 0, 0, 0, 1, 2, 3, 0, 0, 0, 0, 4, 5, 0, 0, 0])
### find nonzeros
z = a!=0
### find switching points
z[1:] ^= z[:-1]
### split at switching points and discard zeros
np.split(a, *np.where(z))[1::2]
# [array([1, 2, 3]), array([4, 5])]