numpy字符串操作 |查找()函数
numpy.core.defchararray.find(arr, sub, start=0, end=None)
是另一个用于在 numpy 中进行字符串操作的函数。它返回字符串中的最低索引,其中为 arr 中的每个元素找到子字符串 sub范围从开始到结束。否则返回 -1。
Parameters:
arr : array_like of str or unicode.
sub : [str or unicode] The substring which to be searched.
start : [ int, optional] The starting location in each string.
end : [ int, optional] The ending location in each string.
Returns : [ndarray] Output array of ints.
代码#1:
# Python program explaining
# numpy.char.find() method
# importing numpy as geek
import numpy as geek
# input arrays
in_arr = geek.array(['aAaAaA', 'baA', 'abBABba'])
print ("Input array : ", in_arr)
# output arrays
out_arr = geek.char.find(in_arr, sub ='A')
print ("Output array: ", out_arr)
输出:
Input array : ['aAaAaA' 'baA' 'abBABba']
Output array: [1 2 3]
代码#2:
# Python program explaining
# numpy.char.find() method
# importing numpy as geek
import numpy as geek
# input arrays
in_arr = geek.array(['aAaAaA', 'aA', 'abBABba'])
print ("Input array : ", in_arr)
# output arrays
out_arr = geek.char.find(in_arr, sub ='a', start = 3, end = 7)
print ("Output array: ", out_arr)
输出:
Input array : ['aAaAaA' 'aA' 'abBABba']
Output array: [ 4 -1 6]