16

I am looking for a numpy function to find the indices at which certain values are found within a vector (xs). The values are given in another array (ys). The returned indices must follow the order of ys.

In code, I want to replace the list comprehension below by a numpy function.

>> import numpy as np
>> xs = np.asarray([45, 67, 32, 52, 94, 64, 21])
>> ys = np.asarray([67, 94])
>> ndx = np.asarray([np.nonzero(xs == y)[0][0] for y in ys]) # <---- This line
>> print(ndx)
[1 4]

Is there a fast way?

Thanks

0

2 Answers 2

22

For big arrays xs and ys, you would need to change the basic approach for this to become fast. If you are fine with sorting xs, then an easy option is to use numpy.searchsorted():

xs.sort()
ndx = numpy.searchsorted(xs, ys)

If it is important to keep the original order of xs, you can use this approach, too, but you need to remember the original indices:

orig_indices = xs.argsort()
ndx = orig_indices[numpy.searchsorted(xs[orig_indices], ys)]
Sign up to request clarification or add additional context in comments.

1 Comment

if you don't need to keep track of what elements where found and which ones where not you can filter the output to get rid of all the indexes beyond limits: ndx = [ e for e in np.searchsorted(xs,ys) if e<len(xs) ]
4

In this kind of cases, just easily use np.isin() function to mask those elements conform your conditions, like this:

xs = np.asarray([45, 67, 32, 52, 94, 64, 21])
ys = np.asarray([67, 94])

mask=xs[np.isin(xs,xy)]
print(xs[mask])

1 Comment

This question does not answer the the question. It ends up printing the value of ys. The variable xy is not defined and should be ys.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.