Python numpy添加数据类型数组
我想添加两个dtype数组并获得相同的dtype数组类型:Python numpy添加数据类型数组,python,numpy,Python,Numpy,我想添加两个dtype数组并获得相同的dtype数组类型: >>> dtype=[('p', '<i8'), ('l', '<f8')] >>> v1 = np.array([(22, 3.14), (4, 0.1)], dtype=dtype) >>> v1 array([(22, 3.14), (4, 0.1)], dtype=[('p', '<i8'), ('l', '<f8')]) >>>
>>> dtype=[('p', '<i8'), ('l', '<f8')]
>>> v1 = np.array([(22, 3.14), (4, 0.1)], dtype=dtype)
>>> v1
array([(22, 3.14), (4, 0.1)], dtype=[('p', '<i8'), ('l', '<f8')])
>>> v2 = np.array([(11, 3.14), (6, 0.2)], dtype=dtype)
>>> v2
array([(11, 3.14), (6, 0.2)], dtype=[('p', '<i8'), ('l', '<f8')])
或
遗憾的是,我不知道有什么比单独计算每一列更简单的方法:
import numpy as np
dtype=[('p', '<i8'), ('l', '<f8')]
v1 = np.array([(22, 3.14), (4, 0.1)], dtype=dtype)
v2 = np.array([(11, 3.14), (6, 0.2)], dtype=dtype)
v = np.empty_like(v1)
for col in v1.dtype.names:
v[col] = v1[col] + v2[col]
print(v)
# [(33L, 6.28) (10L, 0.30000000000000004)]
屈服
p l
0 33 6.28
1 10 0.30
遗憾的是,我不知道有什么比单独计算每一列更简单的方法:
import numpy as np
dtype=[('p', '<i8'), ('l', '<f8')]
v1 = np.array([(22, 3.14), (4, 0.1)], dtype=dtype)
v2 = np.array([(11, 3.14), (6, 0.2)], dtype=dtype)
v = np.empty_like(v1)
for col in v1.dtype.names:
v[col] = v1[col] + v2[col]
print(v)
# [(33L, 6.28) (10L, 0.30000000000000004)]
屈服
p l
0 33 6.28
1 10 0.30
如果保留结构化阵列不是那么重要:
In [703]: v=array(v1.tolist())+array(v2.tolist())
In [704]: v
Out[704]:
array([[ 33. , 6.28],
[ 10. , 0.3 ]])
否则,我能想到的最好方法就是像@unutbu提到的那样逐列添加。如果保留结构化数组不是那么重要的话:
In [703]: v=array(v1.tolist())+array(v2.tolist())
In [704]: v
Out[704]:
array([[ 33. , 6.28],
[ 10. , 0.3 ]])
否则,我能想到的最好方法就是像@unutbu提到的那样逐列添加
In [703]: v=array(v1.tolist())+array(v2.tolist())
In [704]: v
Out[704]:
array([[ 33. , 6.28],
[ 10. , 0.3 ]])