[Numpy-discussion] How to find indices of values in an array (indirect in1d) ?

Peter Creasey p.e.creasey.00 at googlemail.com
Wed Dec 30 15:08:51 EST 2015


>
> In the end, I?ve only the list comprehension to work as expected
>
> A = [0,0,1,3]
> B = np.arange(8)
> np.random.shuffle(B)
> I = [list(B).index(item) for item in A if item in B]
>
>
> But Mark's and Sebastian's methods do not seem to work...
>


The function you want is also in the open source astronomy package
iccpy ( https://github.com/Lowingbn/iccpy ), which essentially does a
variant of Sebastian’s code (which I also couldn’t quite get working),
and handles a few things like old numpy versions (pre 1.4) and allows
you to specify if B is already sorted.

>>> from iccpy.utils import match
>>> print match(A,B)
[ 1  2  0 -1]

Peter



More information about the NumPy-Discussion mailing list