当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Python numpy.index()用法及代码示例


numpy.core.defchararray.index(arr,substring,start = 0,end = None):查找指定范围内子字符串的最低索引,但如果未找到子字符串,则会引发ValueError。

参数:
arr : array-like or string to be searched.
substring : substring to search for.
start, end: [int, optional] Range to search in.

返回: 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]


相关用法


注:本文由纯净天空筛选整理自Mohit Gupta_OMG 大神的英文原创作品 numpy.index() in Python。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。