Xb19
Xb19

Reputation: 130

How to use any() to test if two arrays overlap

I want to test if two arrays have common elements. I tried this but it does not work.

a = np.array([4,5])
b = np.array([1,-1])
a.any() in b

And this returns True...

Upvotes: 3

Views: 3706

Answers (1)

seralouk
seralouk

Reputation: 33147

Use all or any depending on what's your goal:

all(np.isin(a,b))
#or
#np.isin(a, b).all()

or

any(np.isin(a,b))
#or
#np.isin(a, b).any()

Example using all:

a = np.array([1,2])
b = np.array([1,2])

all(np.isin(a,b))
#True

Example using any:

a = np.array([1,2])
b = np.array([1,3])

any(np.isin(a,b))
#True

Upvotes: 4

Related Questions