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

Sebastian Berg sebastian at sipsolutions.net
Wed Dec 30 11:47:44 EST 2015

```On Mi, 2015-12-30 at 17:12 +0100, Nicolas P. Rougier wrote:
> Thanks for the quick answers. I think I will go with the .index and
> list comprehension.
> But if someone finds with a vectorised solution for the numpy 100
> exercises...
>

Yeah, I doubt you can get very pretty, though maybe there is some great
trick. This is one way:

In [67]: A = np.array([2,0,1,4])
In [68]: B = np.array([1,2,0])
In [69]: B_sorter = np.argsort(B)
In [70]: B_index = np.searchsorted(B, A, sorter=B_sorter)
In [71]: invalid = B[B_sorter].take(s, mode='clip') != A
In [72]: B_index[invalid] = -1  # mark invalids with -1
In [73]: B_index
Out[73]: array([ 2,  0,  1, -1])

Anyway, I guess the arrays would likely have to be quite large for this
to beat list comprehension. And maybe doing the searchsorted the other
way around could be faster, no idea.

- Sebastian

>
> Nicolas
>
>
> > On 30 Dec 2015, at 16:31, Benjamin Root <ben.v.root at gmail.com>
> > wrote:
> >
> > Maybe use searchsorted()? I will note that I have needed to do
> > something like this once before, and I found that the list
> > comprehension form of calling .index() for each item was faster
> > than jumping through hoops to vectorize it using searchsorted
> > (needing to sort and then map the sorted indices to the original
> > indices), and was certainly clearer, but that might depend upon the
> > problem size.
> >
> > Cheers!
> > Ben Root
> >
> > On Wed, Dec 30, 2015 at 10:02 AM, Andy Ray Terrel <
> > andy.terrel at gmail.com> wrote:
> > Using pandas one can do:
> >
> > > > > A = np.array([2,0,1,4])
> > > > > B = np.array([1,2,0])
> > > > > s = pd.Series(range(len(B)), index=B)
> > > > > s[A].values
> > array([  1.,   2.,   0.,  nan])
> >
> >
> >
> > On Wed, Dec 30, 2015 at 8:45 AM, Nicolas P. Rougier <
> > Nicolas.Rougier at inria.fr> wrote:
> >
> > I’m scratching my head around a small problem but I can’t find a
> > vectorized solution.
> > I have 2 arrays A and B and I would like to get the indices
> > (relative to B) of elements of A that are in B:
> >
> > > > > A = np.array([2,0,1,4])
> > > > > B = np.array([0,2,0])
> > > > > print (some_function(A,B))
> > [1,2,0]
> >
> > # A[0] == 2 is in B and 2 == B[1] -> 1
> > # A[1] == 0 is in B and 0 == B[2] -> 2
> > # A[2] == 1 is in B and 1 == B[0] -> 0
> >
> > Any idea ? I tried numpy.in1d with no luck.
> >
> >
> > Nicolas
> >
> > _______________________________________________
> > NumPy-Discussion mailing list
> > NumPy-Discussion at scipy.org
> > https://mail.scipy.org/mailman/listinfo/numpy-discussion
> >
> >
> > _______________________________________________
> > NumPy-Discussion mailing list
> > NumPy-Discussion at scipy.org
> > https://mail.scipy.org/mailman/listinfo/numpy-discussion
> >
> >
> > _______________________________________________
> > NumPy-Discussion mailing list
> > NumPy-Discussion at scipy.org
> > https://mail.scipy.org/mailman/listinfo/numpy-discussion
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> https://mail.scipy.org/mailman/listinfo/numpy-discussion
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: This is a digitally signed message part
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20151230/16a6e8cd/attachment.sig>
```

More information about the NumPy-Discussion mailing list