NumPy strings.not_equal()

The numpy.strings.not_equal() function performs an element-wise comparison of two input arrays and returns a boolean array indicating whether the corresponding elements are not equal.

Syntax

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

Parameters

ParameterTypeDescription
x1, x2array_likeInput arrays to compare. If their shapes differ, 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 when performing the comparison.
orderstr, optionalMemory layout order of the output array.
dtypedata-type, optionalSpecifies the data type of the output array.
subokbool, optionalDetermines if subclasses of ndarray are preserved in the output.

Return Value

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


Examples

1. Comparing Two Single Strings

Here, we compare two string values to check if they are different.

</>
Copy
import numpy as np

# Define two strings
string1 = 'apple'
string2 = 'banana'

# Compare using not_equal
result = np.strings.not_equal(string1, string2)

# Print the result
print("Are the strings different?", result)

Output:

Are the strings different? True

2. Comparing Two Arrays of Strings

We compare element-wise two NumPy string arrays.

</>
Copy
import numpy as np

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

# Perform element-wise comparison
result = np.strings.not_equal(arr1, arr2)

# Print the result
print("Element-wise comparison result:", result)

Output:

Element-wise comparison result: [False  True False]

3. Using Broadcasting in String Comparison

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

</>
Copy
import numpy as np

# Define an array of strings
arr = np.array(['apple', 'banana', 'cherry'])

# Define a single string
single_string = 'banana'

# Perform broadcasting comparison
result = np.strings.not_equal(arr, single_string)

# Print the result
print("Comparison with a single string:", result)

Output:

Comparison with a single string: [ True False  True]

4. Using the where Parameter

We use the where parameter to compare only selected elements.

</>
Copy
import numpy as np

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

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

# Perform comparison using where condition
result = np.strings.not_equal(arr1, arr2, where=mask)

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

Output:

Comparison result with mask: [False False False]

The comparison is performed only where mask=True, while other values retain their original state.