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


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。