[Numpy-discussion] Lookup array
Olivier Delalleau
shish@keba...
Mon Oct 10 10:20:08 CDT 2011
2011/10/10 Andrey N. Sobolev <inconnu@list.ru>
> В Mon, 10 Oct 2011 10:03:48 +0100
> Bob Dowling <rjd4+numpy@cam.ac.uk> пишет:
>
> >
> > On 10/10/11 09:53, Andrey N. Sobolev wrote:
> >
> > > 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.
> >
> > May we assume that there are no repeats in A? (i.e. no cases where
> > two different indices are both valid?)
> >
>
> Yes, rows in A are unique and sorted.
> One more typo found - instead of np.which in the previous e-mail it has
> to be np.where, I don't know what I thought about :)
>
> Thanks in advance!
> Andrey
>
The following doesn't use numpy but seems to be about 20x faster:
A_rows = {}
for i, row in enumerate(A):
A_rows[tuple(row)] = i
for i, row in enumerate(B):
C[i] = A_rows.get(tuple(row), -1)
-=- Olivier
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20111010/61ae392b/attachment.html
More information about the NumPy-Discussion
mailing list