我有一个Nx2的数组group
:
array([[ 1, 6],
[ 1, 0],
[ 2, 1],
...,
[40196, 40197],
[40196, 40198],
[40196, 40199]], dtype=uint32)
还有一个数组selection
,其形状为(M,):
array([3216, 3217, 3218, ..., 8039])
我想创建一个新数组,其中包含group
的所有行,这些行中的两个元素都在selection
中。这是我的方法:
np.array([(i,j) for (i,j) in group if i in selection and j in selection])
这个方法可行,但我知道必须有一个更高效的方式来利用一些numpy函数。
4.07 ms ± 197 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
和我的263 ms ± 9.38 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
。 快多了! :) - jenny_wren