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 arrays
myarray1 = np.array(['1', '10', '122'])
myarray2 = np.array(['1', '13', '121'])
# printing the arrays
print(myarray1)
print(myarray2)
# implementing the char.greater() function on both arrays
print(np.char.greater(myarray1, myarray2))

Explanation

  • Line 1: We import the numpy module.
  • Lines 4–5: We use the array() function to create the input arrays, myarray1 and myarray2.
  • Lines 8–9: We print the arrays myarray1 and myarray2.
  • Line 12: We invoke the char.greater() function on both arrays to check if the elements in myarray1 are greater than the elements in myarray2. Next, we print the result to the console.

Free Resources