NumPy strings.greater_equal()

The numpy.strings.greater_equal() function performs an element-wise comparison between two string arrays, returning True where elements in the first array are lexicographically greater than or equal to elements in the second array.

Syntax

</>
Copy
numpy.strings.greater_equal(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True)

Parameters

ParameterTypeDescription
x1, x2array_likeInput string arrays. If their shapes are different, they must be broadcastable to a common shape.
outndarray, None, or tuple of ndarray and None, optionalOptional output array where the result is stored. If None, a new array is created.
wherearray_like, optionalBoolean mask specifying which elements to compare. Elements where where=False retain their original value.
castingstr, optionalDefines the casting behavior for the operation.
orderstr, optionalMemory layout order of the output array.
dtypedata-type, optionalDefines the data type of the output array.
subokbool, optionalDetermines if subclasses of ndarray are preserved in the output.

Return Value

Returns a boolean array where each element is True if the corresponding element in x1 is greater than or equal to x2 lexicographically, otherwise False.


Examples

1. Comparing Two String Arrays

We compare two arrays of fruit names lexicographically.

</>
Copy
import numpy as np

# Define two arrays of fruit names
fruits1 = np.array(["apple", "banana", "cherry"])
fruits2 = np.array(["banana", "apple", "cherry"])

# Perform element-wise greater_equal comparison
result = np.strings.greater_equal(fruits1, fruits2)

# Print the results
print("Fruits1:", fruits1)
print("Fruits2:", fruits2)
print("Comparison result:", result)

Output:

Fruits1: ['apple' 'banana' 'cherry']
Fruits2: ['banana' 'apple' 'cherry']
Comparison result: [False  True  True]

Here, “apple” is lexicographically smaller than “banana” (False), “banana” is greater than “apple” (True), and “cherry” is equal to “cherry” (True).

2. Broadcasting in String Comparisons

We compare an array of strings with a single string, utilizing broadcasting.

</>
Copy
import numpy as np

# Define an array of fruit names
fruits = np.array(["apple", "banana", "cherry", "date"])

# Compare all elements with "banana"
result = np.strings.greater_equal(fruits, "banana")

# Print the results
print("Fruits:", fruits)
print("Comparison with 'banana':", result)

Output:

Fruits: ['apple' 'banana' 'cherry' 'date']
Comparison with 'banana': [False  True  True  True]

Since “apple” is smaller than “banana”, it returns False. Other words are greater or equal to “banana”, so they return True.

3. Using the where Parameter

We selectively compare only certain elements using a condition.

</>
Copy
import numpy as np

# Define two arrays of fruit names
fruits1 = np.array(["apple", "banana", "cherry", "date"])
fruits2 = np.array(["banana", "apple", "date", "cherry"])

# Define a condition mask
mask = np.array([True, False, True, False])

# Perform element-wise greater_equal comparison where mask is True
result = np.strings.greater_equal(fruits1, fruits2, where=mask)

# Print the results
print("Comparison result with mask:", result)

Output:

Comparison result with mask: [False False  True False]

Here, comparisons are only made where mask=True. The other values remain unchanged.