# [Numpy-discussion] intersect1d for N input arrays

Martin Spacek numpy@mspacek.mm...
Thu Oct 15 19:44:42 CDT 2009

```I have a list of many arrays (in my case each is unique, ie has no repeated
elements), and I'd like to extract the intersection of all of them, all in one
go. I'm running numpy 1.3.0, but looking at today's rev of numpy.lib.arraysetops
(http://svn.scipy.org/svn/numpy/trunk/numpy/lib/arraysetops.py), I see
intersect1d has changed. Just a note: the example used in the docstring implies
that the two arrays need to be the same length, which isn't the case. Maybe it
would be good to change the example to two arrays of different lengths.

intersect1d takes exactly 2 arrays. I've modified it a little to take the
intersection of any number of 1D arrays (of any length), in a list or tuple. It
seems to work fine, but could use more testing. Here it is with most of the docs
stripped. Feel free to use it, although I suppose for symmetry, many of the
other functions in arraysetops.py would also have to be modified to work with N
arrays:

def intersect1d(arrays, assume_unique=False):
"""Find the intersection of any number of 1D arrays.
Return the sorted, unique values that are in all of the input arrays.
N = len(arrays)
arrays = list(arrays) # allow assignment
if not assume_unique:
for i, arr in enumerate(arrays):
arrays[i] = np.unique(arr)
aux = np.concatenate(arrays) # one long 1D array
aux.sort() # sorted
shift = N-1
return aux[aux[shift:] == aux[:-shift]]

```