Perform element-wise comparison of two string arrays using a comparison operator in Numpy


To perform element-wise comparison of two string arrays using a comparison operator, use the numpy.compare_chararrays() method in Python Numpy. The arr1 and arr2 are the two input string arrays of the same shape to be compared. The 3rd parameter is rstrip, if True, the spaces at the end of Strings are removed before the comparison.

NumPy offers comprehensive mathematical functions, random number generators, linear algebra routines, Fourier transforms, and more. It supports a wide range of hardware and computing platforms, and plays well with distributed, GPU, and sparse array libraries.

Steps

At first, import the required library −

import numpy as np

Create two One-Dimensional arrays of string −

arr1 = np.array(['Bella', 'Tom', 'John', 'Kate', 'Amy', 'Brad', 'aaa'])
arr2 = np.array(['Cio', 'Tom', 'Cena', 'Kate', 'Adams', 'brad', 'aa'])

Display the arrays −

print("Array 1...
", arr1) print("
Array 2...
", arr2)

Get the type of the arrays −

print("
Our Array 1 type...
", arr1.dtype) print("
Our Array 2 type...
", arr2.dtype)

Get the dimensions of the Arrays −

print("
Our Array 1 Dimensions...
",arr1.ndim) print("
Our Array 2 Dimensions...
",arr2.ndim)

Get the shape of the Arrays −

print("
Our Array 1 Shape...
",arr1.shape) print("
Our Array 2 Shape...
",arr2.shape)

To perform element-wise comparison of two string arrays using a comparison operator, use the numpy.compare_chararrays() method. The arr1 and arr2 are the two input string arrays of the same shape to be compared. The 3rd parameter is rstrip , if True, the spaces at the end of Strings are removed before the comparison −

print("
Result...
",np.compare_chararrays(arr1, arr2, ">", rstrip = True))

Example

import numpy as np

# Create two One-Dimensional arrays of string
arr1 = np.array(['Bella', 'Tom', 'John', 'Kate', 'Amy', 'Brad', 'aaa'])
arr2 = np.array(['Cio', 'Tom', 'Cena', 'Kate', 'Adams', 'brad', 'aa'])

# Display the arrays
print("Array 1...
", arr1) print("
Array 2...
", arr2) # Get the type of the arrays print("
Our Array 1 type...
", arr1.dtype) print("
Our Array 2 type...
", arr2.dtype) # Get the dimensions of the Arrays print("
Our Array 1 Dimensions...
",arr1.ndim) print("
Our Array 2 Dimensions...
",arr2.ndim) # Get the shape of the Arrays print("
Our Array 1 Shape...
",arr1.shape) print("
Our Array 2 Shape...
",arr2.shape) # To perform element-wise comparison of two string arrays using a comparison operator, use the numpy.compare_chararrays() method in Python Numpy # The arr1 and arr2 are the two input string arrays of the same shape to be compared # The 3rd parameter is rstrip , if True, the spaces at the end of Strings are removed before the comparison. print("
Result...
",np.compare_chararrays(arr1, arr2, ">", rstrip = True))

Output

Array 1...
['Bella' 'Tom' 'John' 'Kate' 'Amy' 'Brad' 'aaa']

Array 2...
['Cio' 'Tom' 'Cena' 'Kate' 'Adams' 'brad' 'aa']

Our Array 1 type...
<U5

Our Array 2 type...
<U5

Our Array 1 Dimensions...
1

Our Array 2 Dimensions...
1

Our Array 1 Shape...
(7,)

Our Array 2 Shape...
(7,)

Result...
[False False True False True False True]

Updated on: 07-Feb-2022

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements