[英]TypeError: ufunc 'add' did not contain a loop with signature matching types dtype('<U1') dtype('<U1') dtype('<U1')
I'm a beginner of Python user.我是 Python 用户的初学者。 When I tried to make a code below an Error occurred当我尝试在下面编写代码时发生错误
import numpy as np
np.array(['a', 'b', 'c']) + np.array(['d' ,'e', 'f'])
TypeError: ufunc 'add' did not contain a loop with signature matching types dtype('<U1') dtype('<U1') dtype('<U1')
So I tried to set dtype = '<U1'
, but It didn't work所以我尝试设置dtype = '<U1'
,但它没有用
import numpy as np
np.array(['a', 'b', 'c'], dtype='<U1') + np.array(['d' ,'e', 'f'], dtype='<U1')
How can I concatenate those np.arrays without error?如何无错误地连接那些 np.arrays ?
If you want to concatenate array-wise:如果要以数组方式连接:
a = np.array(['a', 'b', 'c'])
b = np.array(['d' ,'e', 'f'])
c = np.concatenate([a,b])
print(c)
output: output:
['a' 'b' 'c' 'd' 'e' 'f']
If you want to concatenate element-wise:如果要按元素连接:
Method - 1:方法 - 1:
a = np.array(['a', 'b', 'c'])
b = np.array(['d' ,'e', 'f'])
c = np.char.add(a, b)
print(c)
Method - 2:方法 - 2:
a = np.char.array(['a', 'b', 'c'])
b = np.char.array(['d' ,'e', 'f'])
c = a + b
print(c)
output: output:
['ad' 'be' 'cf']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.