NumPy 中元素级字符串多次连接的返回值
要返回元素级字符串多次连接,请在 Python NumPy 中使用 **numpy.char.multiply()** 方法。multiply() 函数根据输入类型返回字符串或 unicode_ 的输出数组。
numpy.char 模块为类型为 numpy.str_ 或 numpy.bytes_ 的数组提供了一组矢量化字符串操作。
步骤
首先,导入所需的库 -
import numpy as np
创建一个字符串的一维数组 -
arr = np.array(['Bella', 'Tom', 'John', 'Kate', 'Amy', 'Brad'])
显示我们的数组 -
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)i 的值是要重复字符串的次数 -
i = 4
要返回元素级字符串多次连接,请使用 numpy.char.multiply() 方法。arr1 和 arr2 是我们两个输入字符串数组 -
print("
Result...
",np.char.multiply(arr,i))示例
import numpy as np
# Create a One-Dimensional array of string
arr = np.array(['Bella', 'Tom', 'John', 'Kate', 'Amy', 'Brad'])
# 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)
# The value of i is the number of times to repeat the string
i = 4
# To return element-wise string multiple concatenation, use the numpy.char.multiply() method in Python Numpy
# The arr1 and arr2 are out two input string arrays
print("
Result...
",np.char.multiply(arr,i))输出
$python3 main.py Array... ['Bella' 'Tom' 'John' 'Kate' 'Amy' 'Brad'] Array datatype... <U5 Array Dimensions... 1 Our Array Shape... (6,) Elements in the Array... 6 Result... ['BellaBellaBellaBella' 'TomTomTomTom' 'JohnJohnJohnJohn' 'KateKateKateKate' 'AmyAmyAmyAmy' 'BradBradBradBrad']
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP