I am trying to find the most efficient way to get the indexes of nested arrays in another array.
import numpy as np
# 0 1 2 3
haystack = np.array([[1,3],[3,4,],[5,6],[7,8]])
needles = np.array([[3,4],[7,8]])
Given the arrays contained in needles
I want to find their indexes in haystack
. In this case 1,3.
I came up with this solution:
indexes = [idx for idx,elem in enumerate(haystack) if elem in needles ]
Which is wrong because actually is sufficient that one element in elem
is in needles
to return the idx
.
Is there any faster alternative?