Python中的 numpy.index()
numpy.core.defchararray.index(arr, substring, start=0, end=None)
:在指定范围内查找子字符串的最低索引但是如果未找到子字符串,则会引发 ValueError。
Parameters:
arr : array-like or string to be searched.
substring : substring to search for.
start, end : [int, optional] Range to search in.
Returns : An integer array with the lowest index of found sub-string, raises ValueError if substring is not found.
代码#1:
# Python Program illustrating
# numpy.char.index() method
import numpy as np
arr = ['this is geeks for geek']
print ("arr : ", arr)
print ("\nindex of 'geeks' : ", np.char.index(arr, 'geeks'))
输出:
arr : ['this is geeks for geek']
index of 'geeks' : [8]
代码#2:
# Python Program illustrating
# numpy.char.index() method
import numpy as np
arr = ['this is geeks for geek']
print ("\nindex of 'geeks' : ", np.char.index(arr, 'geeks', start = 2))
print ("\nindex of 'geeks' : ", np.char.index(arr, 'geeks', start = 10))
print ("\nindex of 'geek' : ", np.char.index(arr, 'geek', start = 10))
输出:
index of 'geeks' : [8]
ValueError: substring not found
index of 'geek' : [18]