Python numpy.index()
numpy.core.defchararray.index(arr, substring, start=0, end=None) :查找指定范围内的子串的最低索引,但是如果没有找到子串,会引发ValueError。
参数:
arr :要搜索的数组类或字符串。
substring :要搜索的子串。
start,end。[int, optional] 要搜索的范围。
返回值 :一个整数数组,包含找到的子串的最低索引,如果没有找到子串,则引发ValueError。
代码 #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]