title: Python: Finding elements in an array that fall within a range
body: <p>I have an array and an integer</p>
  
  <pre><code>import numpy as np
  
  a = np.array([1,2,3,4])
  b = 3
  </code></pre>
  
  <p>I would like to find all elements in <code>a</code> that are within <code>+-diff</code> of <code>b</code>. If <code>diff=1</code> then I can do this using</p>
  
  <pre><code>d = np.where( (a == b - 1) | (a == b) | (a == b + 1) )
  vals = a[d]
  </code></pre>
  
  <p>But what if <code>diff = 2</code>, or is undetermined and I want to create a function that has <code>diff</code> as an input?</p>
  
  <p>I'm sure there must be a far more efficient and flexible way to code this than I have found. </p>
  
  <p>I'm using Python 3.5</p>