w3resource logo
Python Exercises

Python NumPy: Compare two arrays using numpy

Python NumPy: Array Object Exercise-28 with Solution

Write a Python program compare two arrays using numpy.

Sample Solution:-

Python Code:

import numpy as np
a = np.array([1, 2])
b = np.array([4, 5])
print("Array a: ",a)
print("Array b: ",b)
print("a > b")
print(np.greater(a, b))
print("a >= b")
print(np.greater_equal(a, b))
print("a < b")
print(np.less(a, b))
print("a <= b")
print(np.less_equal(a, b))

Sample Output:

Array a:  [1 2]                                                        
Array b:  [4 5]                                                        
a > b                                                                  
[False False]                                                          
a >= b                                                                 
[False False]                                                          
a < b                                                                  
[ True  True]                                                          
a <= b                                                                 
[ True  True]

Python Code Editor:

import numpy as np
a = np.array([1, 2])
b = np.array([4, 5])
print("Array a: ",a)
print("Array b: ",b)
print("a > b")
print(np.greater(a, b))
print("a >= b")
print(np.greater_equal(a, b))
print("a < b")
print(np.less(a, b))
print("a <= b")
print(np.less_equal(a, b))

Improve this sample solution and post your code through Disqus