What is the numpy.char.greater() function in Python?
Overview
In Python, the char.greater() function is used to check, one by one, if the elements of an array x1 are greater than the elements of another array x2 of the same shape. It returns True if x1 is greater than x2 and False if not.
Syntax
char.greater(x1, x2)
Parameters
This function takes two parameter values, x1 and x2, which are the two input arrays of the same shape.
Return value
The char.greater() function returns an output array of bools.
Example
import numpy as np# creating input arraysmyarray1 = np.array(['1', '10', '122'])myarray2 = np.array(['1', '13', '121'])# printing the arraysprint(myarray1)print(myarray2)# implementing the char.greater() function on both arraysprint(np.char.greater(myarray1, myarray2))
Explanation
- Line 1: We import the
numpymodule. - Lines 4–5: We use the
array()function to create the input arrays,myarray1andmyarray2. - Lines 8–9: We print the arrays
myarray1andmyarray2. - Line 12: We invoke the
char.greater()function on both arrays to check if the elements inmyarray1are greater than the elements inmyarray2. Next, we print the result to the console.