📜  Python中的 numpy.char.add()函数

📅  最后修改于: 2022-05-13 01:54:39.037000             🧑  作者: Mango

Python中的 numpy.char.add()函数

NumPy 模块中 char 类的 add() 方法用于str 或 unicode 的两个数组的元素级字符串连接。

numpy.char.add()

示例 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']