Skip to content Skip to sidebar Skip to footer

Count The Number Of Times Elements In A Numpy Array Consecutively Satisfy A Condition

I have a numpy array as follows: import numpy as np a = np.array([1, 4, 2, 6, 4, 4, 6, 2, 7, 6, 2, 8, 9, 3, 6, 3, 4, 4, 5, 8]) and a constant number b=6 I am searching for a numbe

Solution 1:

You can use numpy masking and itertools.groupby.

from itertools import groupby

b = 6
sum(len(list(g))>=2 for i, g in groupby(a < b) if i)
#3

Post a Comment for "Count The Number Of Times Elements In A Numpy Array Consecutively Satisfy A Condition"