Get intersecting rows across two 2D numpy arrays

For short arrays, using sets is probably the clearest and most readable way to do it.

Another way is to use numpy.intersect1d. You’ll have to trick it into treating the rows as a single value, though… This makes things a bit less readable…

import numpy as np

A = np.array([[1,4],[2,5],[3,6]])
B = np.array([[1,4],[3,6],[7,8]])

nrows, ncols = A.shape
dtype={'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)

For large arrays, this should be considerably faster than using sets.

Leave a Comment