Python 替换结构化Numpy数组的字段

Python 替换结构化Numpy数组的字段,python,arrays,numpy,Python,Arrays,Numpy,我已经构建了分支(即某些字段包含子字段)数据类型dtype的numpy数组,如下所示: import numpy dtype_sub = [('sub0', 'i4'), ('sub1', 'f8')] dtype = [('info', [('field0', dtype_sub, 3), ('field1', dtype_sub, 3)])] array = [(tuple([[(0, 0.0) for j in range(3)] for i in ra

我已经构建了分支(即某些字段包含子字段)数据类型
dtype
的numpy数组,如下所示:

import numpy
dtype_sub = [('sub0', 'i4'), ('sub1', 'f8')]
dtype = [('info', [('field0', dtype_sub, 3),
                  ('field1', dtype_sub, 3)])]
array = [(tuple([[(0, 0.0) for j in range(3)] for i in range(2)]),)]
narray = numpy.array(array, dtype = dtype)
在生成和访问所有字段期间,一切正常。 然后我尝试用新值替换其中一个字段。但是,这不起作用:

field0_new = [(1, 1.0) for i in range(3)]
print('--- Original field0: ---')
print(narray[0]['info']['field0'])
print('--- To replace with: ---')
print(field0_new)
narray[0]['info']['field0'] = field0_new
print('--- Replaced field0: ---')
print(narray[0]['info']['field0'])
输出

--- Original field0: ---
[(0, 0.0) (0, 0.0) (0, 0.0)]
--- To replace with: ---
[(1, 1.0), (1, 1.0), (1, 1.0)]
Traceback (most recent call last):
  File "test.py", line 12, in <module>
    narray[0]['info']['field0'] = field0_new
ValueError: cannot copy sequence with size 2 to array axis with dimension 3
--- Original info: ---
([(0, 0.0), (0, 0.0), (0, 0.0)], [(0, 0.0), (0, 0.0), (0, 0.0)])
--- To replace with: ---
[[(1, 1.0), (1, 1.0), (1, 1.0)], [(1, 1.0), (1, 1.0), (1, 1.0)]]
--- Replaced info: ---
([(1, 1.0), (1, 1.0), (1, 1.0)], [(1, 1.0), (1, 1.0), (1, 1.0)])
输出

--- Original field0: ---
[(0, 0.0) (0, 0.0) (0, 0.0)]
--- To replace with: ---
[(1, 1.0), (1, 1.0), (1, 1.0)]
Traceback (most recent call last):
  File "test.py", line 12, in <module>
    narray[0]['info']['field0'] = field0_new
ValueError: cannot copy sequence with size 2 to array axis with dimension 3
--- Original info: ---
([(0, 0.0), (0, 0.0), (0, 0.0)], [(0, 0.0), (0, 0.0), (0, 0.0)])
--- To replace with: ---
[[(1, 1.0), (1, 1.0), (1, 1.0)], [(1, 1.0), (1, 1.0), (1, 1.0)]]
--- Replaced info: ---
([(1, 1.0), (1, 1.0), (1, 1.0)], [(1, 1.0), (1, 1.0), (1, 1.0)])
我注意到,在一种情况下,在字段的打印表示中列表中有逗号(
[(0,0.0),(0,0.0),(0,0.0)]
),而在另一种情况下,列表中没有逗号(
[(0,0.0)(0,0.0)(0,0.0)]
)。然而,我不能理解这一点,因为它们应该是同一领域的表示

有人能帮忙解决这个问题吗

编辑:

在玩了一会儿之后,我注意到将元组和列表传递给复杂的结构化数组有双重含义:

info_new = [[(i * j, 10.5 + j) for j in range(3)] for i in range(2)]
导致错误

--- To replace with: ---
[[(0, 10.5), (0, 11.5), (0, 12.5)], [(0, 10.5), (1, 11.5), (2, 12.5)]]
--- Replaced info: ---
([(0, 0.0), (0, 0.0), (0, 0.0)], [(10, 10.5), (10, 10.5), (10, 10.5)])

预期产出

--- To replace with: ---
([(0, 10.5), (0, 11.5), (0, 12.5)], [(0, 10.5), (1, 11.5), (2, 12.5)])
--- Replaced info: ---
([(0, 10.5), (0, 11.5), (0, 12.5)], [(0, 10.5), (1, 11.5), (2, 12.5)])
但是,我仍然无法理解语法是如何工作的,以及为什么直接替换
field0
不起作用

编辑2:

目前我有两个自己的想法(加上沃伦·韦克瑟的答案):

1) 将其作为较大字段的一部分传递:

info_new = tuple([field0_new, narray[0]['info']['field1']])
narray[0]['info'] = info_new
2) 分部分传递:

for i in range(len(narray[0]['info']['field0'])):
    narray[0]['info']['field0'][i] = field0_new[i]
然而,所有这些解决方案都相当草率,不具有普遍性


仍在等待语法歧义解释的正确答案。

如果先对字段进行索引,然后对数组序列进行索引,则会起作用:

narray['info']['field0'][0] = field0_new
(更好的答案是调查原因,但这就是我目前得到的全部。)