What is an easy way to check if 2 arrays have at least one common element? Using numpy is possible, but not necessary.
The code I have found so far only checks for specific generic elements. While I only need to check the True or False condition.
Assuming the input arrays will be Aand B, you can use np.in1dwith np.any, also -
A
B
np.in1d
np.any
import numpy as np np.in1d(A,B).any()
You can also use NumPy broadcasting capability, for example:
NumPy broadcasting capability
(A.ravel()[:,None] == B.ravel()).any()
You can use any:
any
any(x in set(b) for x in a)
, , , set(b) a, :
set(b)
a
sb = set(b) any(x in sb for x in a)
, b - ( a):
b
(smaller,bigger) = sorted([a,b], key=len) sbigger = set(bigger) any(x in sbigger for x in smaller)
I would go with kits .
def doArraysIntersect(array1, array2): return bool(set(array1) & set(array2))
def lists_overlap(a, b) for i in a: if i in b: return True return False
Source: https://habr.com/ru/post/1610417/More articles:How to check lowercase letters in indexOf - javascriptWhat does A => => B mean? - scalaApache Spark RDD and Java 8: Exception Handling - javaCompose Try Better - scalaWhich constructor initializes the x3 variable? - javaAccess parameter from anonymous class - javaWhy do C have these strange translation limits (section 2.2.4.1)? - cAccessing a shadow variable from an anonymous class - javaSelect ListViewItem when the child has UWP focus - c #TypeScript Functor - typescriptAll Articles