Approach #1
We could use broadcasting
-
A[(np.abs(np.subtract.outer(A,B)) > 0.04).all(1)]
Approach #2
We could leverage searchsorted
to have a generic numpy.isin
with tolerance specifier for use in generic problems, like so -
def isin_tolerance(A, B, tol):
A = np.asarray(A)
B = np.asarray(B)
Bs = np.sort(B) # skip if already sorted
idx = np.searchsorted(Bs, A)
linvalid_mask = idx==len(B)
idx[linvalid_mask] = len(B)-1
lval = Bs[idx] - A
lval[linvalid_mask] *=-1
rinvalid_mask = idx==0
idx1 = idx-1
idx1[rinvalid_mask] = 0
rval = A - Bs[idx1]
rval[rinvalid_mask] *=-1
return np.minimum(lval, rval) <= tol
Hence, to solve our case -
out = A[~isin_tolerance(A, B, tol=0.04)]
Sample run -
In [294]: A
Out[294]: array([13.8357, 3.245 , 3.8357])
In [295]: B
Out[295]: array([5.6132, 3.2415, 3.6086, 3.5666, 3.8769, 4.3587])
In [296]: A[~isin_tolerance(A, B, tol=0.04)]
Out[296]: array([13.8357, 3.8357])
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…