當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python numpy.char.add()用法及代碼示例

NumPy模塊中char類的add()方法用於str或unicode的兩個數組的逐元素字符串連接。

numpy.char.add()

用法:numpy.char.add(x1, x2)

參數:
  • x1:要連接的第一個數組(在開頭連接)
  • x2:要連接的第二個數組(在末尾連接)

Returns:字符串或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)

輸出:

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





相關用法


注:本文由純淨天空篩選整理自Yash_R大神的英文原創作品 numpy.char.add() function in Python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。