title: Find the indices of the lowest closest neighbors between two lists in python
body: <p>Given 2 numpy arrays of unequal size: A (a presorted dataset) and B (a list of query values). I want to find the closest "lower" neighbor in array A to each element of array B. Example code below:</p>
  
  <pre><code>import numpy as np
  
  A = np.array([0.456, 2.0, 2.948, 3.0, 7.0, 12.132]) #pre-sorted dataset
  B = np.array([1.1, 1.9, 2.1, 5.0, 7.0]) #query values, not necessarily sorted
  print A.searchsorted(B)
  # RESULT:  [1 1 2 4 4]
  # DESIRED: [0 0 1 3 4]
  </code></pre>
  
  <p>In this example, B[0]'s closest neighbors are A[0] and A[1]. It is closest to A[1], which is why searchsorted returns index 1 as a match, but what i want is the lower neighbor at index 0. Same for B[1:4], and B[4] should be matched with A[4] because both values are identical.</p>
  
  <p>I could do something clunky like this:</p>
  
  <pre><code>desired = []
  for b in B:
      id = -1
      for a in A:
          if a &gt; b:
              if id == -1:
                  desired.append(0)
              else:
                  desired.append(id)
              break
  
          id+=1
  
  print desired
  # RESULT: [0, 0, 1, 3, 4]
  </code></pre>
  
  <p>But there's gotta be a prettier more concise way to write this with numpy. I'd like to keep my solution in numpy because i'm dealing with large data sets, but i'm open to other options.</p>