python: check if an numpy array contains any element of another array

后端 未结 3 1205
长情又很酷
长情又很酷 2021-02-13 12:08

What is the best way to check if an numpy array contains any element of another array?

example:

array1 = [10,5,4,13,10,1,1,22,7,3,15,9]
array2 = [3,4,9,1         


        
3条回答
  •  离开以前
    2021-02-13 13:06

    You can try this

    >>> array1 = [10,5,4,13,10,1,1,22,7,3,15,9]
    >>> array2 = [3,4,9,10,13,15,16,18,19,20,21,22,23]
    >>> set(array1) & set(array2)
    set([3, 4, 9, 10, 13, 15, 22])
    

    If you get result means there are common elements in both array.

    If result is empty means no common elements.

提交回复
热议问题