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]
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]
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
[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".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]