0

In order to get the index corresponding to the "99" value in a numpy array, we do :

mynumpy=([5,6,9,2,99,3,88,4,7))
np.where(my_numpy==99)

What if, I want to get the index corresponding to the following values 99,55,6,3,7? Obviously, it's possible to do it with a simple loop but I'm looking for a more vectorization solution. I know Numpy is very powerful so I think it might exist something like that.

desired output :

searched_values=np.array([99,55,6,3,7])
np.where(searched_values in mynumpy)
[(4),(),(1),(5),(8)]
5
  • 1
    check this. stackoverflow.com/questions/32191029/… Commented Mar 2, 2018 at 10:28
  • @Kasramvd Re-opening because this question additionally needs the masking for the elements that are not present in searched_values. Hope that looks fair. Commented Mar 2, 2018 at 10:32
  • Yeah Divakar's solution takes into account the special case where values are not present Commented Mar 2, 2018 at 10:45
  • Can there be multiple values? Commented Mar 2, 2018 at 17:42
  • A similar question involving multiple values, How to get a list of indexes selected by a specific value efficiently with numpy arrays?. The accepted answer used a default dictionary. Commented Mar 2, 2018 at 18:43

1 Answer 1

2

Here's one approach with np.searchsorted -

def find_indexes(ar, searched_values, invalid_val=-1):
    sidx = ar.argsort()
    pidx = np.searchsorted(ar, searched_values, sorter=sidx)
    pidx[pidx==len(ar)] = 0
    idx = sidx[pidx]
    idx[ar[idx] != searched_values] = invalid_val
    return idx

Sample run -

In [29]: find_indexes(mynumpy, searched_values, invalid_val=-1)
Out[29]: array([ 4, -1,  1,  5,  8])

For a generic invalid value specifier, we could use np.where -

def find_indexes_v2(ar, searched_values, invalid_val=-1):
    sidx = ar.argsort()
    pidx = np.searchsorted(ar, searched_values, sorter=sidx)
    pidx[pidx==len(ar)] = 0
    idx = sidx[pidx]
    return np.where(ar[idx] == searched_values, idx, invalid_val)

Sample run -

In [35]: find_indexes_v2(mynumpy, searched_values, invalid_val=None)
Out[35]: array([4, None, 1, 5, 8], dtype=object)

# For list output
In [36]: find_indexes_v2(mynumpy, searched_values, invalid_val=None).tolist()
Out[36]: [4, None, 1, 5, 8]
Sign up to request clarification or add additional context in comments.

5 Comments

Waou, thks a lot, indeed it looks like to another question, but you take into account special cases so I do not know what to do.
@hansglick Not sure what you meant by - "do not know what to do" thing :)
Just see your stackoverflow profile, how can I send you a private message?
What if it the element is present several times. I would like to be able to retrieve all the indexes. find_indexes(np.array([5,11,5]),np.array([5])) . Desired output [(0,2)]
@hansglick, you should have included some duplicates in the original question.

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.