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


Python numpy.find()用法及代碼示例


numpy.core.defchararray.find(arr, substring, start=0, end=None)查找指定範圍內子字符串的最低索引。

參數:
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.



代碼1:

# Python Program illustrating  
# numpy.char.find() method  
import numpy as np  
  
arr = ['vdsdsttetteteAAAa', 'AAAAAAAaattttds', 'AAaaxxxxtt', 'AAaaXDSDdscz'] 
  
print ("arr:", arr) 
  
print ("\nfind of 'tt'", np.char.find(arr, 'tt')) 
print ("find of 'tt'", np.char.find(arr, 'tt', start = 0)) 
print ("find of 'tt'", np.char.find(arr, 'tt', start = 8))

輸出:

arr: ['vdsdsttetteteAAAa', 'AAAAAAAaattttds', 'AAaaxxxxtt', 'AAaaXDSDdscz']

find of 'tt' [ 5  9  8 -1]
find of 'tt' [ 5  9  8 -1]
find of 'tt' [ 8  9  8 -1]


代碼2:

# Python Program illustrating  
# numpy.char.find() method  
import numpy as np  
  
arr = ['vdsdsttetteteAAAa', 'AAAAAAAaattttds', 'AAaaxxxxtt', 'AAaaXDSDdscz'] 
  
  
print ("\nfind of 'Aa'", np.char.find(arr, 'Aa')) 
print ("find of 'Aa'", np.char.find(arr, 'Aa', start = 8))

輸出:

find of 'Aa' [15  6  1  1]
find of 'Aa' [15 -1 -1 -1]


相關用法


注:本文由純淨天空篩選整理自Mohit Gupta_OMG 大神的英文原創作品 numpy.find() in Python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。