中的一般策略numpy.lib.recfunctions
是按字段名称将数据从原始结构复制到新结构。
举个简单的例子:
In [186]: x = np.array([('one',1),('two',2)], dtype='U3,int')
In [187]: x
Out[187]: array([('one', 1), ('two', 2)], dtype=[('f0', '<U3'), ('f1', '<i4')])
In [188]: y = np.empty(2, dtype=x.dtype.descr+[('three',float)])
In [189]: y
Out[189]:
array([('', 0, 0.), ('', 0, 0.)],
dtype=[('f0', '<U3'), ('f1', '<i4'), ('three', '<f8')])
In [190]: for n in x.dtype.names:
...: y[n] = x[n]
...:
In [191]: y
Out[191]:
array([('one', 1, 0.), ('two', 2, 0.)],
dtype=[('f0', '<U3'), ('f1', '<i4'), ('three', '<f8')])
在 1.14 中,我对你的分配有错误。
In [192]: z = np.array(x, dtype=y.dtype)
ValueError: structures must have the same size
我不记得在早期版本中尝试过这个。