With A as the input array and tol as the tolerance value, we could have a vectorized approach with NumPy broadcasting , as well -
A[~(np.triu(np.abs(A[:,None] - A) <= tol,1)).any(0)]
Run Example -
In [20]: A = np.array([2.1, 1.3 , 1.9 , 1.1 , 2.0 , 2.5 , 2.9]) In [21]: tol = 0.3 In [22]: A[~(np.triu(np.abs(A[:,None] - A) <= tol,1)).any(0)] Out[22]: array([ 2.1, 1.3, 2.5, 2.9])
Note that 1.9 gone because we had 2.1 within a tolerance of 0.3 . Then 1.1 went for 1.3 and 2.0 for 2.1 .
Note that this will create a unique array with a chain-proximity check. As an example:
In [91]: A = np.array([ 1.1, 1.3, 1.5, 2. , 2.1, 2.2, 2.35, 2.5, 2.9]) In [92]: A[~(np.triu(np.abs(A[:,None] - A) <= tol,1)).any(0)] Out[92]: array([ 1.1, 2. , 2.9])
Thus, 1.3 left due to 1.1 and 1.5 left due to 1.3 .