对于短数组,使用集合可能是最清晰,最易读的方法。
另一种方法是使用
numpy.intersect1d。不过,您必须欺骗它,以将行作为单个值来对待……这使事情的可读性降低了……
import numpy as npA = np.array([[1,4],[2,5],[3,6]])B = np.array([[1,4],[3,6],[7,8]])nrows, ncols = A.shapedtype={'names':['f{}'.format(i) for i in range(ncols)], 'formats':ncols * [A.dtype]}C = np.intersect1d(A.view(dtype), B.view(dtype))# This last bit is optional if you're okay with "C" being a structured array...C = C.view(A.dtype).reshape(-1, ncols)对于大型数组,这应该比使用集合快得多。



