What is the numpy.char.rfind() function from NumPy in Python?
Overview
The char.rfind() function in Python returns the highest index in an input array's string where the sub substring is found. This is such that the sub is contained within [start, end].
Syntax
char.rfind(a, sub, start=0, end=None)
Syntax for the char.rfind() function
Parameter value
The char.rfind() function takes the following parameter values:
a: This is the input array. It is required.sub: This is the substring to be searched for. It is required.start,end: These are optional arguments interpreted as in slice notation.
Return value
The char.rfind() function returns an output array of integers. It returns -1 if the sub is not found.
Example
import numpy as np# creating an input arraya = np.array(['What', "makes you", "happy?"])# creating the substringsub = "ou"# implementing the char.rfind() functionmyarray = np.char.rfind(a, sub, start=1, end=None)# printing the input arrayprint(a)# printing the output arraysprint(myarray)
Explanation
numpy module.a using the array() function.sub.char.rfind() function on the input array. The result is assigned to a variable myarray.a.myarray.Notice from the code output that the substring"ou"can be found in the array's third element. Its index position in the element is7.