擅长:python、mysql、java
<p>从X中选取近似匹配:</p>
<pre><code>import numpy as np
X = np.array([7,22,34,49,56,62,76,82,89,161,174]) #len:11
Y = np.array([7,14,49,57,66,76,135,142,161]) #len:9
dist = np.abs(Y[:, np.newaxis] - X)
#print(dist)
for i in range(len(Y)):
for j in dist[i]:
if -3<=j<=3: #approximation of 3
idx = dist[i].tolist().index(j)
print(X[idx])
</code></pre>
<p><strong>输出:</strong></p>
<pre><code>7
49
56
76
161
</code></pre>
<p>从Y中选取近似匹配:</p>
<pre><code>import numpy as np
X = np.array([7,22,34,49,56,62,76,82,89,161,174]) #len:11
Y = np.array([7,14,49,57,66,76,135,142,161]) #len:9
dist = np.abs(X[:, np.newaxis] - Y)
#print(dist)
for i in range(len(Y)+1):
for j in dist[i]:
if -3<=j<=3:
#print(j)
idx = dist[i].tolist().index(j)
print(Y[idx])
</code></pre>
<p><strong>输出:</strong></p>
<pre><code>7
49
57
76
161
</code></pre>