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