Python中的 numpy.char.multiply()函数
NumPy 模块中 char 类的 multiply() 方法用于逐元素字符串多重连接。
numpy.char.multiply()
Syntax : numpy.char.multiply(a, i)
Parameters :
- a : array of str or unicode
- i : number of times to be repeated
Returns : Array of strings
示例 1:在单个元素字符串数组上使用该方法。
Python3
# importing the module
import numpy as np
# created an array
arr1 = np.array(['Geeks'])
print("Original Array :")
print(arr1)
# number of times to be repeated
i = 3
# using the char.multiply() method
arr2 = np.char.multiply(arr1, i)
print("\nNew array :")
print(arr2)
Python3
# importing the module
import numpy as np
# created an array
arr1 = np.array(['Geeks', 'for', 'Geeks'])
print("Original Array :")
print(arr1)
# number of times to be repeated
i = 2
# using the char.multiply() method
arr2 = np.char.multiply(arr1, i)
print("\nNew array :")
print(arr2)
输出 :
Original Array :
['Geeks']
New array :
['GeeksGeeksGeeks']
示例 2:在多元素字符串数组上使用该方法。
Python3
# importing the module
import numpy as np
# created an array
arr1 = np.array(['Geeks', 'for', 'Geeks'])
print("Original Array :")
print(arr1)
# number of times to be repeated
i = 2
# using the char.multiply() method
arr2 = np.char.multiply(arr1, i)
print("\nNew array :")
print(arr2)
输出 :
Original Array :
['Geeks' 'for' 'Geeks']
New array :
['GeeksGeeks' 'forfor' 'GeeksGeeks']