

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Compare and return True if an array is less than another array in Numpy
To compare and return True if an array is less than another array, use the numpy.char.less() method in Python Numpy. The arr1 and arr2 are the two input string arrays of the same shape.
Unlike numpy.greater, 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(['Bella', 'Tom', 'John', 'Kate', 'Amy', 'Brad', 'aaa']) arr2 = np.array(['Cio', 'Tom', 'Cena', 'Kate', 'Adams', 'brad', 'aa'])
Display the arrays −
print("Array 1...\n", arr1) print("\nArray 2...\n", arr2)
Get the type of the arrays −
print("\nOur Array 1 type...\n", arr1.dtype) print("\nOur Array 2 type...\n", arr2.dtype)
Get the dimensions of the Arrays −
print("\nOur Array 1 Dimensions...\n",arr1.ndim) print("\nOur Array 2 Dimensions...\n",arr2.ndim)
Get the shape of the Arrays −
print("\nOur Array 1 Shape...\n",arr1.shape) print("\nOur Array 2 Shape...\n",arr2.shape)
To compare and return True if an array is less than another array, use the numpy.char.less() method. The arr1 and arr2 are the two input string arrays of the same shape −
print("\nResult...\n",np.char.less(arr1,arr2))
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...\n", arr1) print("\nArray 2...\n", arr2) # Get the type of the arrays print("\nOur Array 1 type...\n", arr1.dtype) print("\nOur Array 2 type...\n", arr2.dtype) # Get the dimensions of the Arrays print("\nOur Array 1 Dimensions...\n",arr1.ndim) print("\nOur Array 2 Dimensions...\n",arr2.ndim) # Get the shape of the Arrays print("\nOur Array 1 Shape...\n",arr1.shape) print("\nOur Array 2 Shape...\n",arr2.shape) # To compare and return True if an array is less than another array, use the numpy.char.less() method in Python Numpy # The arr1 and arr2 are the two input string arrays of the same shape. print("\nResult...\n",np.char.less(arr1,arr2))
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... [ True False False False False True False]
- Related Questions & Answers
- Compare and return True if a Numpy array is less than equal to another
- Compare and return True if an array is greater than another array in Numpy
- Compare and return True if a Numpy array is greater than equal to another
- Return the truth value of an array less than another element-wise in Numpy
- Return the truth value of an array less than equal to another element-wise in Numpy
- Return element-wise True where signbit is set (less than zero) in Numpy
- Compare and return True if two string arrays are equal in Numpy
- Compare and return True if two string Numpy arrays are not equal
- Return the truth value of an array greater than another element-wise in Numpy
- Mask array elements less than a given value in Numpy
- Return the truth value of an array greater than equal to another element-wise in Numpy
- Return element-wise True where signbit is set (less than zero) and store the result in a new location in Numpy
- Mask an array where less than or equal to a given value in Numpy
- Check which element in a masked array is less than the given value in Numpy
- JavaScript Determine the array having majority element and return TRUE if its in the same array