當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。