Python Comparing Arrays

We can use the relational operators >, >=, <, <=, == and != to compare the arrays of same size. These operators compare the corresponding elements of the arrays and return another array with Boolean type values. It means the resultant array contains elements which are True or False.

compare arrays examples

A Python program to compare two arrays and display the resultant Boolean type array.

CopiedCopy Code

from numpy import * 
a = array([1,2,3,0]) 
b = array([0,2,3,1]) 
c = a == b 
print('Result of a==b:', c) 
c = a > b 
print('Result of a>b:', c) 
c = a <= b 
print('Result of a<=b:', c)
                        

The any() function can be used to determine if any one element of the array is True.

The all() function can be used to determine whether all the elements in the array are True . The any() and all() functions return either True or False.

Python program to know the effects of any() and all() functions.

CopiedCopy Code

from numpy import * 
a = array([1,2,3,0]) 
b = array([0,2,3,1]) 
c = a > b 
print('Result of a>b:', c) 
print('Check if any one element is true:', any(c)) 
print('Check if all elements are true:', all(c)) 
if(any(a>b)): 
print('a contains atleast one element greater than those of b')
							
							

The functions logical_and(), logical_or() and logical_not() are useful to get the Boolean array as a result of comparing the compound condition.

CopiedCopy Code

from numpy import * 
a = array([1,2,3], int) 
b = array([0,2,3], int) 
c = logical_and(a>0, a<4) 
print(c) 
c = logical_or(b>=0, b==1) 
print(c) 
c = logical_not(b) 
print(c)
							
							

The where() function can be used to create a new array based on whether a given condition is True or False. The syntax of the where() function is:

array = where(condition, expression1, expression2)

If the 'condition' is true, 'expression1' is executed and the result is stored into the array, else 'expression2' is executed and its result is stored into the array. For example,

a = array([10, 21, 30, 41, 50], int)

c = where(a%2==0, a, 0)

The new array 'c' contains elements from 'a' based on the condition a%2==0. This condition is applied to each element of the array 'a' and if it is True, the element of 'a' is stored into 'c' else '0' is stored into c. Hence the array 'c' looks like this:

[10 0 30 0 50].

Python program to compare the corresponding elements of two arrays and retrieve the biggest elements.

CopiedCopy Code

from numpy import * 
a = array([10, 20, 30, 40, 50], int) 
b = array([1, 21, 3, 40, 51], int)
c = where(a>b, a, b) 
print(c)
	                    

The nonzero() function is useful to know the positions of elements which are non zero. This function returns an array that contains the indexes of the elements of the array which are not equal to zero. For example,

a = array([1, 2, 0, -1, 0, 6], int)

In the preceding array, the elements which are non zero are: 1, 2, -1 and 6. Their positions or indexes are: 0, 1, 3, 5. These indexes can be retrieved into another array 'c' as:

c = nonzero(a)

CopiedCopy Code

from numpy import * 
a = array([1, 2, 0, -1, 0, 6], int)
c = nonzero(a)
#display indexes
for i in c: 
  print(i)
#display non zero values
print(a[c])