[Numpy-discussion] Finding values in an array

Julian Taylor jtaylor.debian at googlemail.com
Fri Nov 28 03:40:29 EST 2014

```On 28.11.2014 09:37, Robert Kern wrote:
> On Fri, Nov 28, 2014 at 8:22 AM, Julian Taylor
> <jtaylor.debian at googlemail.com <mailto:jtaylor.debian at googlemail.com>>
> wrote:
>>
>> On 28.11.2014 04:15, Alexander Belopolsky wrote:
>> > I probably miss something very basic, but how given two arrays a and b,
>> > can I find positions in a where elements of b are located?  If a were
>> > sorted, I could use searchsorted, but I don't want to get valid
>> > positions for elements that are not in a.  In my case, a has unique
>> > elements, but in the general case I would accept the first match.  In
>> > other words, I am looking for an array analog of list.index() method.
>>
>> np.where(np.in1d(a, b))
>
> Only if the matching elements in `b` have the same order as they do in `a`.
>

seems to work also if unordered:
In [32]: a = np.arange(1000)

In [33]: b = np.arange(500,550, 3)

In [34]: np.random.shuffle(a)

In [35]: np.random.shuffle(b)

In [36]: np.where(np.in1d(a, b))
Out[36]:
(array([  0, 106, 133, 149, 238, 398, 418, 498, 533, 541, 545, 589, 634,
798, 846, 891, 965]),)

```

More information about the NumPy-Discussion mailing list