My understanding of your problem is that you are trying to find the closest elements for each A[n][1] in a different set of points ( B[i][1] limited to points if A[n][0] is in within +/- 0.5 from B[i][0] ).
(I'm not familiar with numpy or scipy, and I'm sure there is a better way to do this with their algorithms.)
Saying this, here is my naive implementation in O(a*b*log(a*b)) time.
def main(a,b): for a_bound,a_val in a: dist_to_valid_b_points = {abs(a_val-b_val):(b_bound,b_val) for b_bound,b_val in b if are_within_bounds(a_bound,b_bound)} print get_closest_point((a_bound, a_val),dist_to_valid_b_points) def are_within_bounds(a_bound, b_bound): return abs(b_bound-a_bound) < 0.5 def get_closest_point(a_point, point_dict): return (a_point, None if not point_dict else point_dict[min(point_dict, key=point_dict.get)])
main([[50,140],[51,180],[54,500]],[[50.1, 170], [51,200],[55,510]]) displays the following result:
((50, 140), (50.1, 170)) ((51, 180), (51, 200)) ((54, 500), None)