I am trying to find patterns in a numpy
array
, called values
. I\'d like to return the starting index position of the pattern. I know I
Couldn't you simply use np.where
(assuming this is the optimal way to find an element) and then only check pattens which satisfy the first condition.
import numpy as np
values = np.array([0,1,2,1,2,4,5,6,1,2,1])
searchval = [1,2]
N = len(searchval)
possibles = np.where(values == searchval[0])[0]
solns = []
for p in possibles:
check = values[p:p+N]
if np.all(check == searchval):
solns.append(p)
print(solns)