w3resource logo
Python Exercises

Python NumPy: Test if specified values are present in an array

Python NumPy: Array Object Exercise-65 with Solution

Write a Python program to test if specified values are present in an array.

Sample Solution:-

Python Code:

import numpy as np
x = np.array([[1.12, 2.0, 3.45], [2.33, 5.12, 6.0]], float) 
print("Original array:")
print(x)
print(2 in x)
print(0 in x)
print(6 in x)
print(2.3 in x)
print(5.12 in x)

Sample Output:

Original array:                                                        
[[ 1.12  2.    3.45]                                                   
 [ 2.33  5.12  6.  ]]                                                  
True                                                                   
False                                                                  
True                                                                   
False                                                                  
True    

Python Code Editor:

import numpy as np
x = np.array([[1.12, 2.0, 3.45], [2.33, 5.12, 6.0]], float) 
print("Original array:")
print(x)
print(2 in x)
print(0 in x)
print(6 in x)
print(2.3 in x)
print(5.12 in x)

Improve this sample solution and post your code through Disqus