Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in Data Science by (18.4k points)

Can anyone have a more NumPy-like way of accomplishing this:

def uneven_compare(array1, array2):

    return numpy.all([numpy.any(array2 == elem) for elem in array1])

I want to check if all of the elements in one array present in a second array.

1 Answer

0 votes
by (36.8k points)
edited by

You can use this:

In [6]: array1 = np.array([0, 1, 2, 5, 0])

In [7]: array2 = np.array([0, 10, 20, 1, 2, 30, 5])

In [8]: np.in1d(array1, array2)

Out[8]: array([ True,  True,  True,  True,  True], dtype=bool)

In [9]: np.all(np.in1d(array1, array2))

Out[9]: True

Learn Python for Data Science Course to improve your technical knowledge.

Browse Categories