[Numpy-discussion] How to find indices of values in an array (indirect in1d) ?
ben.v.root at gmail.com
Wed Dec 30 10:31:00 EST 2015
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.
On Wed, Dec 30, 2015 at 10:02 AM, Andy Ray Terrel <andy.terrel at gmail.com>
> 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([1,2,0])
>> >>> print (some_function(A,B))
>> # A == 2 is in B and 2 == B -> 1
>> # A == 0 is in B and 0 == B -> 2
>> # A == 1 is in B and 1 == B -> 0
>> Any idea ? I tried numpy.in1d with no luck.
>> NumPy-Discussion mailing list
>> NumPy-Discussion at scipy.org
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the NumPy-Discussion