Reputation: 1
Let's suppose I have two numpy.array():
a = np.array([1,2,3,4,5,6,7,8,9,10,11,12]) #index
b = np.array([1,1,4,3,3,3,5,2,2,2,2,6]) #element
How do I efficiently find all consequent same element whose number of consequent same element >=3 , the start_index and end_index? I hope the result :
c: array([4,8]) #start_index
d: array([6,11]) #end_index
e: array([3,2]) #consequent same values whose number of consequent same element >=3
Upvotes: 0
Views: 301
Reputation: 53119
Find the group boundaries:
sw, = np.where(np.diff(b, prepend=1, append=1))
Filter for condition:
cm, = np.where(np.diff(sw)>=3)
Retrieve corresponding indices
c = a[sw[cm]]
d = a[sw[cm+1]-1]
and values
e = b[sw[cm]]
Admire
c
# array([4, 8])
d
# array([ 6, 11])
e
# array([3, 2])
Upvotes: 1