[编辑将更简单的方法放在首位。]
在实践中,我可能会这样做:
In [166]: d = dict(m_out[:,::-1])
In [167]: [(k, d[k]) for k in eqn_out if k in d]
Out[167]: [(7, 6), (9, 8), (11, 10), (13, 12), (15, 14), (17, 16)]
但是为了好玩,坚持使用 numpy,比如:
[更新:更好的 numpy 方法]:
In [15]: m_out[np.in1d(v, eqn_out)][:, ::-1]
Out[15]:
array([[ 7, 6],
[ 9, 8],
[11, 10],
[13, 12],
[15, 14],
[17, 16]])
或者我原来的 numpy 方法:
In [150]: import numpy as np
In [151]: m_out = np.arange(50).reshape(25,2)
In [152]: v = m_out[:,1]
In [153]: eqn_out = np.arange(7, 18)
In [154]: eqn_out
Out[154]: array([ 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17])
只保留我们知道的价值观:
In [155]: np.intersect1d(eqn_out, v)
Out[155]: array([ 7, 9, 11, 13, 15, 17])
查找它们所在的位置(假设数据已排序!!):
In [156]: v.searchsorted(np.intersect1d(eqn_out, v))
Out[156]: array([3, 4, 5, 6, 7, 8])
使用这些索引进行选择:
In [157]: m_out[v.searchsorted(np.intersect1d(eqn_out, v))]
Out[157]:
array([[ 6, 7],
[ 8, 9],
[10, 11],
[12, 13],
[14, 15],
[16, 17]])
翻动:
In [158]: m_out[v.searchsorted(np.intersect1d(eqn_out, v))][:,::-1]
Out[158]:
array([[ 7, 6],
[ 9, 8],
[11, 10],
[13, 12],
[15, 14],
[17, 16]])