在Python中,对于每个元素,返回字符串中子字符串找到的最高索引
要在字符串中返回子字符串sub找到的最高索引,请在Python NumPy中使用numpy.char.rfind()方法。该方法返回整数输出数组。如果未找到sub,则返回-1。第一个参数是输入数组。第二个参数是要搜索的子字符串。
步骤
首先,导入所需的库:
import numpy as np
创建一个一维字符串数组:
arr = np.array(['KATIE', 'JOHN', 'AmY', 'BRATleuATp'])
显示我们的数组:
print("Array...\n",arr)获取数据类型:
print("\nArray datatype...\n",arr.dtype)
获取数组的维度:
print("\nArray Dimensions...\n",arr.ndim)获取数组的形状:
print("\nOur Array Shape...\n",arr.shape)获取数组的元素个数:
print("\nNumber of elements in the Array...\n",arr.size)要返回字符串中子字符串sub找到的最高索引,请使用numpy.char.rfind()方法:
print("\nResult (rfind)...\n",np.char.rfind(arr, 'AT'))
示例
import numpy as np
# Create a One-Dimensional array of strings
arr = np.array(['KATIE', 'JOHN', 'AmY', 'BRATleuATp'])
# Displaying our array
print("Array...\n",arr)
# Get the datatype
print("\nArray datatype...\n",arr.dtype)
# Get the dimensions of the Array
print("\nArray Dimensions...\n",arr.ndim)
# Get the shape of the Array
print("\nOur Array Shape...\n",arr.shape)
# Get the number of elements of the Array
print("\nNumber of elements in the Array...\n",arr.size)
# To return the highest index in the string where substring sub is found, use the numpy.char.rfind() method in Python Numpy
print("\nResult (rfind)...\n",np.char.rfind(arr, 'AT'))输出
Array... ['KATIE' 'JOHN' 'AmY' 'BRATleuATp'] Array datatype... <U10 Array Dimensions... 1 Our Array Shape... (4,) Number of elements in the Array... 4 Result (rfind)... [ 1 -1 -1 7]
广告
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP