python - Why numpy returns true index while comparing integer and float value from two different list -
how avoid returning true index while comparing 10.5
, 10
?
a = np.array([1,2,3,4,5,6,7,8,9,10.5]) b = np.array([1,7,10]) = np.searchsorted(a,b) print # [0 6 9]
i want places of exact matches: [0 6]
you use np.searchsorted
left
, right
, keep don't return same index both:
>>> import numpy np >>> = np.array([1,2,3,4,5,6,7,8,9,10.5]) >>> b = np.array([1,7,10]) >>> = np.searchsorted(a, b, 'left') >>> j = np.searchsorted(a, b, 'right') >>> i[i!=j] array([0, 6], dtype=int64)
that works because searchsorted
returns index element needs inserted if want keep other array sorted. when value present in other array returns index before match (left
) , index after matches(right
). if index differs there's exact match , if index same there's no exact match
Comments
Post a Comment