[Numpy-discussion] Lookup array

Andrey N. Sobolev inconnu@list...
Mon Oct 10 03:53:54 CDT 2011

Hi all,

I have 2 arrays - A with the dimensions of 1000x4 and B with the
dimensions of 5000x4. B doesn't (hopefully) contain any rows that are
not in A. I need to create a lookup array C, the i-th value of which
will be the index of B[i] in A. In the (very rare) case when B[i] is not
in A C[i] should be equal to -1. Now I'm dealing with it using which:
import numpy as np
for i, row in enumerate(B):
        C[i] = np.which(np.all(A == row, axis = 1))[0][0]
    except (IndexError, ):
        C[i] = -1
but that's very slow (it consumes 70% of cpu time needed by the whole
program). I guess that it's because of a slow pythonic loop, but I
just can't get how to get rid of it. Any suggestions would be

Thanks in advance,

General and theoretical physics dept., South Ural State University
454080, Pr. Lenina, 76, Chelyabinsk, Russia
Tel: +7 351 265-47-13

More information about the NumPy-Discussion mailing list