NumPy: Compare two given arrays

NumPy: Array Object Exercise-28 with Solution

Write a NumPy program compare two given arrays.

Pictorial Presentation:

Python NumPy: Compare two given arrays.

Sample Solution:

NumPy 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:

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

Previous: Write a NumPy program to find the indices of the maximum and minimum values along the given axis of an array.
Next: Write a NumPy program to sort an along the first, last axis of an array.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz

Python: Tips of the Day

Python: While-else construct

i = 5

while i > 1:
    print("Whil-ing away!")
    i -= 1
    if i == 3:
    print("Finished up!")