Finding Patterns in a Numpy Array

后端 未结 5 452
忘了有多久
忘了有多久 2021-01-05 18:28

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

5条回答
  •  别那么骄傲
    2021-01-05 19:04

    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)
    

提交回复
热议问题