w3resource

NumPy: Test whether each element of a 1-D array

NumPy: Array Object Exercise-17 with Solution

Write a NumPy program to test whether each element of a 1-D array is also present in a second array.

Pictorial Presentation:

Python NumPy: Test whether each element of a 1-D array

Sample Solution:-

Python Code:

import numpy as np
array1 = np.array([0, 10, 20, 40, 60])
print("Array1: ",array1)
array2 = [0, 40]
print("Array2: ",array2)
print("Compare each element of array1 and array2")
print(np.in1d(array1, array2))

Sample Output:

Array1:  [ 0 10 20 40 60]                                              
Array2:  [0, 40]                                                       
Compare each element of array1 and array2                              
[ True False False  True False]

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a NumPy program to find the number of elements of an array, length of one array element in bytes and total bytes consumed by the elements
Next: Write a NumPy program to find common values between two arrays.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz



Python: Tips of the Day

Set comprehension:

>>> m = {x ** 2 for x in range(5)}
>>> m
{0, 1, 4, 9, 16}