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
Based on a previous question I can count the number c
which is defined by the number of times the elements in a
are less than b
2 or more times consecutively.
from itertools import groupby
b = 6
sum(len(list(g))>=2 for i, g in groupby(a < b) if i)
so in this example c == 3
Now I would like to output an array each time the condition is met instead of counting the number of times the condition is met.
So with this example the right output would be:
array1 = [1, 4, 2]
array2 = [4, 4]
array3 = [3, 4, 4, 5]
since:
1, 4, 2, 6, 4, 4, 6, 2, 7, 6, 2, 8, 9, 3, 6, 3, 4, 4, 5, 8 # numbers in a
1, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1, 0 # (a<b)
^^^^^^^-----^^^^-----------------------------^^^^^^^^^^--- # (a<b) 2+ times consecutively
1 2 3
So far I have tried different options:
np.isin((len(list(g))>=2 for i, g in groupby(a < b)if i), a)
and
np.extract((len(list(g))>=2 for i, g in groupby(a < b)if i), a)
But none of them achieved what I am searching for. Can someone point me to the right Python tools in order to output the different arrays satisfying my condition?