Skip to content Skip to sidebar Skip to footer

Python - Return Intersection Of Two Arrays

I have two arrays and I am trying to return a new array that equals the intersection of my original two arrays. The two original arrays should be of the same length. For example, i

Solution 1:

If you want to keep duplicates, like in your examples, you can use a list comprehension:

defintersection(list_a, list_b):
    return [ e for e in list_a if e in list_b ]

which produces:

in:
    [(255, 255, 255), (255, 255, 255)]
    [(255, 255, 255), (255, 255, 255)]
out:
    [(255, 255, 255), (255, 255, 255)]

in:
    [(100, 100, 100), (255, 255, 255)]
    [(255, 255, 255), (255, 255, 255)]
out:
    [(255, 255, 255)]

If you want uniquie combinations between the lists (sets) though:

def intersection(a, b):
    return list(set(a).intersection(b))

which produces:

in:
    [(255, 255, 255), (255, 255, 255)]
    [(255, 255, 255), (255, 255, 255)]
out:
    [(255, 255, 255)]

in:
    [(100, 100, 100), (255, 255, 255)]
    [(255, 255, 255), (255, 255, 255)]
out:
    [(255, 255, 255)]

Cheers!

Solution 2:

Not sure how big your arrays will get, but if they remain fairly small, this could work:

import numpy as np

arr1 = np.array([(255, 255, 255), (255, 255, 255)])
arr2 = np.array([(255, 255, 255), (255, 255, 255)])
intersectedArr = []

for a1, a2 in zip(arr1, arr2):
    if np.array_equal(a1, a2):
        intersectedArr.append(a1)
print(np.array(intersectedArr))

arr1 = np.array([(100, 100, 100), (255, 255, 255)])
arr2 = np.array([(255, 255, 255), (255, 255, 255)])
intersectedArr = []

for a1, a2 in zip(arr1, arr2):
    if np.array_equal(a1, a2):
        intersectedArr.append(a1)
print(np.array(intersectedArr))

Solution 3:

how about a numpy answer?

import numpy as np


arr1 = np.array([(255, 255, 255), (255, 255, 25)])  # changed some to 25
arr2 = np.array([(255, 25, 255), (255, 255, 25)])

arr1[np.where(arr1==arr2)]

array([255, 255, 255, 255,  25])

2nd example

arr1 = np.array([(100, 100, 100), (255, 255, 255)])
arr2 = np.array([(255, 255, 255), (255, 255, 255)])

arr1[np.where(arr1==arr2)]

array([255, 255, 255])

Solution 4:

NOTE: This assumes [a, b, c] and [b, c, a] gives [a, b, c], that is the order of elements is ignored.

OK, I've done a little experimenting and this might be what you are after. Given:

arr1a = np.array([(255, 255, 255), (255, 255, 255)])
arr1b = np.array([(100, 100, 100), (255, 255, 255)])
arr2 = np.array([(255, 255, 255), (255, 255, 255)])

Then we can find an intersection with:

np.array([item in arr2 for item in arr1a])

ie, for each element in arr1a, check to see it appears in arr2 also. This gives a result of:

>>>array([ True,  True], dtype=bool)

Similarly:

np.array([item in arr2 for item in arr1b])
>>> array([False,  True], dtype=bool)

Now, we can use this result to pick the common values from the original lists:

mask = np.array([item in arr2 for item in arr1a])
arr1a[mask]
>>> array([[255, 255, 255],
           [255, 255, 255]])

And:

mask = np.array([item in arr2 for item in arr1b])
arr1b[mask]
>>> array([[255, 255, 255]])

Solution 5:

For larger arrays it might help to use pandas' groupby and cumcount:

In [11]: df1 = pd.DataFrame(arr1)

In [12]: df1["cumcount"] = df1.groupby([0, 1, 2]).cumcount()

In [13]: df1
Out[13]:
     012  cumcount
0100100100012552552550

In [14]: df2 = pd.DataFrame(arr2)

In [15]: df2["cumcount"] = df2.groupby([0, 1, 2]).cumcount()

In [16]: df2
Out[16]:
     012  cumcount
0255255255012552552551

Now a merge gets you the array you desire:

In [21]: df1.merge(df1).iloc[:, :3].values
Out[21]:
array([[100, 100, 100],
       [255, 255, 255]])

In [22]: df1.merge(df2).iloc[:, :3].values
Out[22]: array([[255, 255, 255]])

In [23]: df2.merge(df2).iloc[:, :3].values
Out[23]:
array([[255, 255, 255],
       [255, 255, 255]])

Post a Comment for "Python - Return Intersection Of Two Arrays"