NumPy strings.less()

The numpy.strings.less() function compares two string arrays element-wise and returns True where elements in the first array are lexicographically less than elements in the second array.

Syntax

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

Parameters

ParameterTypeDescription
x1, x2array_likeInput arrays containing strings. If their shapes differ, they must be broadcastable to a common shape.
outndarray, None, or tuple of ndarray and None, optionalOptional output array where results are stored. If None, a new array is created.
wherearray_like, optionalBoolean mask that determines where comparisons occur.
castingstr, optionalDefines type casting behavior.
orderstr, optionalSpecifies memory layout of the output array.
dtypedata-type, optionalDefines the output data type.
subokbool, optionalDetermines if subclasses of ndarray are preserved in the output.

Return Value

Returns an array of boolean values indicating whether elements in x1 are lexicographically less than elements in x2. If both inputs are scalars, a single boolean value is returned.


Examples

1. Comparing Two Single Strings

We compare two string values lexicographically.

</>
Copy
import numpy as np

# Define two strings
str1 = "apple"
str2 = "banana"

# Compare the strings using numpy.strings.less()
result = np.strings.less(str1, str2)

# Print the result
print(f'Is "{str1}" less than "{str2}"? {result}')

Output:

Is "apple" less than "banana"? True

2. Comparing Two Arrays of Strings

We compare two arrays of strings element-wise.

</>
Copy
import numpy as np

# Define two arrays of strings
arr1 = np.array(["apple", "banana", "cherry"])
arr2 = np.array(["banana", "apple", "date"])

# Compare the arrays using numpy.strings.less()
result = np.strings.less(arr1, arr2)

# Print the results
print("First array:", arr1)
print("Second array:", arr2)
print("Comparison result:", result)

Output:

First array: ['apple' 'banana' 'cherry']
Second array: ['banana' 'apple' 'date']
Comparison result: [ True False  True]

3. Using the out Parameter

Storing the comparison result in a predefined output array.

</>
Copy
import numpy as np

# Define input arrays
arr1 = np.array(["apple", "banana", "cherry"])
arr2 = np.array(["banana", "apple", "date"])

# Create an output array of the same shape
output_array = np.empty(arr1.shape, dtype=bool)

# Compare strings and store the result in output_array
np.strings.less(arr1, arr2, out=output_array)

# Print the results
print("Comparison result stored in output array:", output_array)

Output:

Comparison result stored in output array: [ True False  True]

4. Using the where Parameter

Performing the comparison only at specific indices.

</>
Copy
import numpy as np

# Define input arrays
arr1 = np.array(["apple", "banana", "cherry"])
arr2 = np.array(["banana", "apple", "date"])

# Define a mask (compare only where True)
mask = np.array([True, False, True])

# Perform comparison only where mask is True
result = np.strings.less(arr1, arr2, where=mask)

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

Output:

Comparison result with mask: [ True False  True]

Elements where mask=False retain their previous values, while only mask=True positions undergo comparison.