NumPy strings.greater()
The numpy.strings.greater()
function performs an element-wise comparison of two input string arrays and returns a boolean array indicating whether the corresponding elements of x1
are lexicographically greater than those in x2
.
Syntax
</>
Copy
numpy.strings.greater(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True)
Parameters
Parameter | Type | Description |
---|---|---|
x1, x2 | array_like | Input string arrays. They must either have the same shape or be broadcastable to a common shape. |
out | ndarray, None, or tuple of ndarray and None, optional | Optional output array where the result is stored. If None, a new array is created. |
where | array_like, optional | Boolean mask specifying which elements to compare. Unselected elements retain their original value. |
casting | str, optional | Defines the casting behavior for data types. |
order | str, optional | Memory layout order of the output array. |
dtype | data-type, optional | Defines the data type of the output array. |
subok | bool, optional | Determines if subclasses of ndarray are preserved in the output. |
Return Value
Returns a boolean array or scalar indicating the result of lexicographical comparison (x1 > x2
) for each element.
Examples
1. Comparing Two Single Strings
Checking if one string is lexicographically greater than another.
</>
Copy
import numpy as np
# Define two strings
str1 = "apple"
str2 = "banana"
# Compare lexicographically
result = np.strings.greater(str1, str2)
# Print the result
print(f'Is "{str1}" greater than "{str2}"? :', result)
Output:
Is "apple" greater than "banana"? : False

2. Comparing Two Arrays of Strings
Comparing corresponding elements in two arrays lexicographically.
</>
Copy
import numpy as np
# Define two string arrays
arr1 = np.array(["apple", "cherry", "banana"])
arr2 = np.array(["banana", "apple", "banana"])
# Perform element-wise comparison
result = np.strings.greater(arr1, arr2)
# Print the results
print("Array 1:", arr1)
print("Array 2:", arr2)
print("Comparison Result:", result)
Output:
Array 1: ['apple' 'cherry' 'banana']
Array 2: ['banana' 'apple' 'banana']
Comparison Result: [False True False]

3. Using Broadcasting for Comparison
Comparing a single string with an array of strings using broadcasting.
</>
Copy
import numpy as np
# Define a single string and an array
single_str = "banana"
arr = np.array(["apple", "banana", "cherry"])
# Perform element-wise comparison
result = np.strings.greater(arr, single_str)
# Print the results
print("Array:", arr)
print(f'Comparison with "{single_str}":', result)
Output:
Array: ['apple' 'banana' 'cherry']
Comparison with "banana": [False False True]

4. Using the where
Parameter
Applying a condition to control which elements are compared.
</>
Copy
import numpy as np
# Define two string arrays
arr1 = np.array(["apple", "cherry", "banana"])
arr2 = np.array(["banana", "apple", "banana"])
# Define a mask (compare only selected elements)
mask = np.array([True, False, True])
# Compare elements where mask is True
result = np.strings.greater(arr1, arr2, where=mask)
# Print the results
print("Array 1:", arr1)
print("Array 2:", arr2)
print("Comparison Result with Mask:", result)
Output:
Array 1: ['apple' 'cherry' 'banana']
Array 2: ['banana' 'apple' 'banana']
Comparison Result with Mask: [False False False]

The comparison is only performed where the mask is True
. Unmasked elements retain their original value.