3

How to avoid returning true index while comparing 10.5 and 10?

A = np.array([1,2,3,4,5,6,7,8,9,10.5])

B = np.array([1,7,10])

i = np.searchsorted(A,B)

print i   # [0 6 9]

I want to get the places of the exact matches: [0 6]

4
  • So are you asking for how to avoid returning 9 in then? Search sorted will always return a value, it's not checking if they are equal, it's telling you where you could insert 10 into the list to maintain its order. Commented Aug 20, 2017 at 7:50
  • In this case I want it shall return two index as it is matching exactly at 0 and 6th place . Commented Aug 20, 2017 at 8:22
  • Then what is best way to find index when exactly there is a match. And how can I get the indexes of smaller list so that I can do kind of A[I]-B[j]. Where j is found from match and I is the index of bigger list . Is it possible to get these ? Commented Aug 20, 2017 at 8:24
  • And of course in this case it shall return zero Commented Aug 20, 2017 at 8:29

2 Answers 2

2

You could use np.searchsorted with left and right and only keep those who don't return the same index for both:

>>> import numpy as np
>>> A = np.array([1,2,3,4,5,6,7,8,9,10.5])
>>> B = np.array([1,7,10])

>>> i = np.searchsorted(A, B, 'left')
>>> j = np.searchsorted(A, B, 'right')
>>> i[i!=j]
array([0, 6], dtype=int64)

That works because searchsorted returns the index where the element needs to be inserted if you want to keep the other array sorted. So when the value is present in the other array it returns the index before the match (left) and the index after the matches(right). So if the index differs there's an exact match and if the index is the same there's no exact match

Sign up to request clarification or add additional context in comments.

8 Comments

I have tried A = np.array([1,2,3,4,5,6,7,8,9,10.5]) B = np.array([1,7,10]) c=set(A) indx=[i for i, item in enumerate(B) if item in c] Now I want to subtract each value of matched index from higher list. the results is ofcourse zero. How to perform A[index]-c[indx]
Not sure what you mean. Probably better to ask a new question instead of following a different thread in the comments.
No I am in the same page of the problem . I got indexes but how can I subtract each value of matched indexes from another one. The result ofcourse would be zero
Yes Sir, I want to implement this
@Poka It's still not clear to me what you want to do. What exactly should be the result? [0, 0] or just 0 or something else? What's the higher list? These are all things that can't be solved in the comments it would be much easier to ask a new question where you exactly state what you want to get and how that would be "determined".
|
0

First you can help searchsorted, since A is sorted, for better complexity:

A = np.array([1,2,3,4,5,6,7,8,9,10.5])
B = np.array([1,7,10])    
i = np.searchsorted(A,B,sorter=range(len(A)))

then in1d can find the exact correspondence :

j = i[np.in1d(A[i],B,True)]
# [0,6] 

1 Comment

Thanks . I got the indices correctly. But problem is one is list of let say length 10 and another is list of length of 7. And there is a match of 5 elements. I can get index of either lower list or higher list. But now how can I subtract each matched indexes from another

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.