在NumPy中使用分隔符作为分隔符字符串返回字符串中的单词列表
要使用分隔符作为分隔符字符串返回字符串中的单词列表,请在 Python NumPy 中使用 **numpy.char.split()** 方法:
- 第一个参数是输入数组
- 第二个参数是分隔符
如果给出 maxsplit 参数,则最多进行 maxsplit 次分割。split() 函数返回一个列表对象数组。
numpy.char 模块为 numpy.str_ 或 numpy.bytes_ 类型数组提供了一组矢量化字符串操作。
步骤
首先,导入所需的库:
import numpy as np
创建一个数组:
arr = np.array(["Bella-Cio", "Brad-Pitt", "Katie-Perry"])
显示我们的数组:
print("Array...
",arr)获取数据类型:
print("
Array datatype...
",arr.dtype)
获取数组的维度:
print("
Array Dimensions...
",arr.ndim)获取数组的形状:
print("
Our Array Shape...
",arr.shape)获取数组的元素个数:
print("
Elements in the Array...
",arr.size)要使用分隔符作为分隔符字符串返回字符串中的单词列表,请使用 numpy.char.split() 方法:
print("
Result...
",np.char.split(arr, '-'))
示例
import numpy as np
# Create an array
arr = np.array(["Bella-Cio", "Brad-Pitt", "Katie-Perry"])
# Displaying our Array
print("Array...
",arr)
# Get the datatype
print("
Array datatype...
",arr.dtype)
# Get the dimensions of the Array
print("
Array Dimensions...
",arr.ndim)
# Get the shape of the Array
print("
Our Array Shape...
",arr.shape)
# Get the number of elements of the Array
print("
Elements in the Array...
",arr.size)
# To return a list of the words in the string using separator as the delimiter string, use the numpy.char.split() method in Python Numpy
# The 1st parameter is the input array
# The 2nd parameter is the separator
print("
Result...
",np.char.split(arr, '-'))输出
Array... ['Bella-Cio' 'Brad-Pitt' 'Katie-Perry'] Array datatype... <U11 Array Dimensions... 1 Our Array Shape... (3,) Elements in the Array... 3 Result... [list(['Bella', 'Cio']) list(['Brad', 'Pitt']) list(['Katie', 'Perry'])]
广告
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP