Python中的 numpy.char.add()函数
NumPy 模块中 char 类的 add() 方法用于str 或 unicode 的两个数组的元素级字符串连接。
numpy.char.add()
Syntax : numpy.char.add(x1, x2)
Parameters :
- x1 : first array to be concatenated (concatenated at the beginning)
- x2 : second array to be concatenated (concatenated at the end)
Returns : Array of strings or unicode
示例 1:在 2 个单元素字符串数组上使用该方法。
Python3
# importing the module
import numpy as np
# create the arrays
x1 = ['Hello']
x2 = ['World']
print("The arrays are : ")
print(x1)
print(x2)
# using the char.add() method
result = np.char.add(x1, x2)
print("\nThe concatenated array is :")
print(result)
Python3
# importing the module
import numpy as np
# create the arrays
x1 = ['Hello', 'to', 'all']
x2 = ['Geeks', 'for', 'Geeks']
print("The arrays are : ")
print(x1)
print(x2)
# using the char.add() method
result = np.char.add(x1, x2)
print("\nThe concatenated array is :")
print(result)
输出 :
The arrays are :
['Hello']
['World']
The concatenated array is :
['HelloWorld']
示例 2:在 2 个多元素字符串数组上使用该方法。
Python3
# importing the module
import numpy as np
# create the arrays
x1 = ['Hello', 'to', 'all']
x2 = ['Geeks', 'for', 'Geeks']
print("The arrays are : ")
print(x1)
print(x2)
# using the char.add() method
result = np.char.add(x1, x2)
print("\nThe concatenated array is :")
print(result)
输出 :
The arrays are :
['Hello', 'to', 'all']
['Geeks', 'for', 'Geeks']
The concatenated array is :
['HelloGeeks' 'tofor' 'allGeeks']