Numpy字符串操作 | rindex()函数
numpy.core.defchararray.rindex()函数,当找不到子字符串 sub 时引发 ValueError。按元素调用 str.rindex。
Syntax : numpy.core.defchararray.rindex(arr, sub, start = 0, end = None)
Parameters :
arr : [array-like of str or unicode] Array-like of str .
sub : [str or unicode] Input string or unicode.
start, end : [int, optional] Optional arguments start and end are interpreted as in slice notation.
Return : Return the output array of ints.
代码#1:
Python3
# Python program explaining
# numpy.char.rindex() function
# importing numpy as geek
import numpy as geek
arr = "GeeksforGeeks - A computer science portal for geeks"
sub = 'science'
gfg = geek.char.rindex(arr, sub)
print (gfg)
Python3
# Python program explaining
# numpy.char.rindex() function
# importing numpy as geek
import numpy as geek
arr = "GeeksforGeeks - A computer science portal for geeks"
sub = 'geeks'
gfg = geek.char.rindex(arr, sub, start = 0, end = None)
print (gfg)
输出 :
27
代码#2:
Python3
# Python program explaining
# numpy.char.rindex() function
# importing numpy as geek
import numpy as geek
arr = "GeeksforGeeks - A computer science portal for geeks"
sub = 'geeks'
gfg = geek.char.rindex(arr, sub, start = 0, end = None)
print (gfg)
输出 :
46