- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 two string arrays are equal in Numpy
To compare and return True if two string arrays are equal, use the numpy.char.equal() method in Python Numpy. The arr1 and arr2 are the two input string arrays of the same shape. Unlike numpy.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(['Bella', 'Tom', 'John', 'Kate', 'Amy', 'Brad']) arr2 = np.array(['Cio', 'Tom', 'Cena', 'Kate', 'Adams', 'brad'])
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 two string arrays are equal, use the numpy.char.equal() method. The arr1 and arr2 are the two input string arrays of the same shape −
print("
Result...
",np.char.equal(arr1,arr2))
Example
import numpy as np # Create two One-Dimensional arrays of string arr1 = np.array(['Bella', 'Tom', 'John', 'Kate', 'Amy', 'Brad']) arr2 = np.array(['Cio', 'Tom', 'Cena', 'Kate', 'Adams', 'brad']) # 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 two string arrays are equal, use the numpy.char.equal() method in Python Numpy # The arr1 and arr2 are the two input string arrays of the same shape. print("
Result...
",np.char.equal(arr1,arr2))
Output
Array 1... ['Bella' 'Tom' 'John' 'Kate' 'Amy' 'Brad'] Array 2... ['Cio' 'Tom' 'Cena' 'Kate' 'Adams' 'brad'] Our Array 1 type... <U5 Our Array 2 type... <U5 Our Array 1 Dimensions... 1 Our Array 2 Dimensions... 1 Our Array 1 Shape... (6,) Our Array 2 Shape... (6,) Result... [False True False True False False]
- Related Articles
- Compare and return True if two string Numpy arrays are not equal
- Return True if all entries of two arrays are equal in Numpy
- Return True if two Numpy arrays are element-wise equal within a tolerance
- Compare and return True if a Numpy array is less than equal to another
- Compare and return True if a Numpy array is greater than equal to another
- Compare two arrays and return the element-wise minimum in Numpy
- Compare two arrays and return the element-wise maximum in Numpy
- Compare two Numpy arrays and return the element-wise maximum with fmax()
- Compare two Numpy arrays and return the element-wise minimum ignoring NaNs
- Compare two Numpy arrays and return the element-wise minimum with fmin()
- Compare two arrays and return the element-wise maximum ignoring NaNs in Numpy
- Compare and return True if an array is less than another array in Numpy
- Compare and return True if an array is greater than another array in Numpy
- Compare two Numpy arrays with some Inf values and return the element-wise minimum
- Compare two arrays with some Inf values and return the element-wise maximum in Numpy
