## Searching numpy array for for pattern

I'd like to find a value in a numpy array given a search pattern. For instance for the given array `a`

, I want to retrieve a result of `1`

when using the search pattern `s`

because `1`

is the element at index 0 of `a[:,1]`

(=`array([1, 0, 0, 1])`

) and the elements of `a[1:,1]`

match `s`

(i.e. `(a[1:,1] == s).all() == True`

=> return `a[0,1]`

).

Another example would be `s=[1, 0, 1]`

for which I would expect a search result of `2`

(match at 4th column starting (1-based)). `2`

would also be the search result for `s=[2, 0, 0]`

, etc.

```
>>> import numpy as np
>>> a = np.asarray([[0, 1, 2, 2, 2, 2, 2, 2], [0, 0, 1, 1, 2, 2, 3, 3], [0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 1, 0, 1, 0, 1]])
>>> a
array([[0, 1, 2, 2, 2, 2, 2, 2],
[0, 0, 1, 1, 2, 2, 3, 3],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 1, 0, 1, 0, 1]])
>>> s = np.asarray([0, 0, 1])
```

I came up with `a[0, np.where((a[1:,:].transpose() == s).all(axis=-1))[0][0]]`

, but thought there must be something more elegant...

Additionally, it would be great if I could do this operation with one call on multiple search patters, so that I retrieve the 0-element for which the values of index 1 to index 3 match.

Show source

## Answers to Searching numpy array for for pattern ( 1 )

Single search patternHere's one approach with help from

`broadcasting`

and`slicing`

-Multiple search patternsFor multiple search patterns (stored as 2D array), we just need to

`broadcast`

as before and look for`ANY`

match at the end -Here's a sample run -