Find numpy vectors in a set quickly
<p>I have a numpy array, for example:</p>

<pre><code>a = np.array([[1,2],
              [3,4],
              [6,4],
              [5,3],
              [3,5]])
</code></pre>

<p>and I also have a set</p>

<pre><code>b = set((1,2),(6,4),(9,9))
</code></pre>

<p>I want to find the index of vectors that exist in set b, here is</p>

<pre><code>[0, 2]
</code></pre>

<p>but I use a for loop to implement this, is there a convinient way to do this job avoiding for loop?
The for loop method I used:</p>

<pre><code>record = []
for i in range(a.shape[0]):
    if (a[i, 0], a[i, 1]) in b:
        record.append(i)
</code></pre>

<p>A one line solution using a list comprehension:</p>

<pre><code>In [62]: a = np.array([[1,2],
    ...:               [3,4],
    ...:               [6,4],
    ...:               [5,3],
    ...:               [3,5]])

In [63]: b = set(((1,2),(6,4),(9,9)))
In [64]: where([tuple(e) in b for e in a])[0]
Out[64]: array([0, 2])
</code></pre>

python numpy set vectorization lookup