NumPy strings.less_equal()
The numpy.strings.less_equal()
function compares two string arrays element-wise and returns a boolean array indicating whether each element in x1
is less than or equal to the corresponding element in x2
.
Syntax
</>
Copy
numpy.strings.less_equal(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 to compare. If their shapes differ, they must 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. Elements where where=False retain their original value. |
casting | str, optional | Defines the casting behavior when comparing strings. |
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 an array of boolean values indicating where x1 <= x2
element-wise. If both inputs are scalars, a single boolean value is returned.
Examples
1. Comparing Two Scalar Strings
Here, we compare two string values directly.
</>
Copy
import numpy as np
# Define two string scalars
str1 = "apple"
str2 = "banana"
# Compare the strings using less_equal
result = np.strings.less_equal(str1, str2)
# Print the result
print("Is 'apple' <= 'banana'? :", result)
Output:
Is 'apple' <= 'banana'? : True

2. Comparing Arrays of Strings
We compare element-wise in two string arrays.
</>
Copy
import numpy as np
# Define two arrays of strings
arr1 = np.array(["apple", "cherry", "banana"])
arr2 = np.array(["banana", "apple", "cherry"])
# Compare element-wise
result = np.strings.less_equal(arr1, arr2)
# Print the result
print("Array 1:", arr1)
print("Array 2:", arr2)
print("Comparison result:", result)
Output:
Array 1: ['apple' 'cherry' 'banana']
Array 2: ['banana' 'apple' 'cherry']
Comparison result: [ True False True]

3. Broadcasting a Single String Against an Array
We compare a single string with each element in an array.
</>
Copy
import numpy as np
# Define a single string and an array of strings
single_str = "banana"
str_array = np.array(["apple", "banana", "cherry"])
# Compare the single string with each array element
result = np.strings.less_equal(single_str, str_array)
# Print the result
print("Single String:", single_str)
print("String Array:", str_array)
print("Comparison result:", result)
Output:
Single String: banana
String Array: ['apple' 'banana' 'cherry']
Comparison result: [False True True]

4. Using the where
Parameter
Using a mask to selectively compare only certain elements.
</>
Copy
import numpy as np
# Define two arrays of strings
arr1 = np.array(["apple", "cherry", "banana"])
arr2 = np.array(["banana", "apple", "cherry"])
# Define a mask (only compare where mask is True)
mask = np.array([True, False, True])
# Compare with the where parameter
result = np.strings.less_equal(arr1, arr2, where=mask)
# Print the result
print("Comparison result with mask:", result)
Output:
Comparison result with mask: [ True False True]

The comparison is only performed for elements where mask=True
. Other values remain unchanged.