Compare and Return True if a Numpy Array is Less Than or Equal to Another



To compare and return True if an array is less than equal to another, use the numpy.char.less_equal() method in Python Numpy. The arr1 and arr2 are the two input string arrays of the same shape.

Unlike numpy.less_equal, this comparison is performed by first stripping whitespace characters from the end of the string. This behavior is provided for backward-compatibility with numarray.

The numpy.char module provides a set of vectorized string operations for arrays of type numpy.str_ or numpy.bytes_.

Steps

At first, import the required library −

import numpy as np

Create two One-Dimensional arrays of string −

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

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 compare and return True if an array is less than equal to another, use the numpy.char.less_equal() method. The arr1 and arr2 are the two input string arrays of the same shape −

print("Result...",np.char.less_equal(arr1,arr2))

Example

Open Compiler
import numpy as np # Create two One-Dimensional arrays of string arr1 = np.array(['Cio', 'Tom', 'Cena', 'Kate', 'Adams', 'brad', 'aa']) arr2 = np.array(['Bella', 'Tom', 'John', 'Kate', 'Amy', 'Brad', 'aaa']) # 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 compare and return True if an array is less than equal to another, use the numpy.char.less_equal() method in Python Numpy # The arr1 and arr2 are the two input string arrays of the same shape. print("Result...",np.char.less_equal(arr1,arr2))

Output

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

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

Our Array 1 type...
Updated on: 2022-02-22T07:49:38+05:30

341 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements