Python NumPy:使用自定义数据类型时的数组分配问题

Python NumPy:使用自定义数据类型时的数组分配问题,python,arrays,multidimensional-array,numpy,Python,Arrays,Multidimensional Array,Numpy,我发现NumPy和ndarray的自定义数据类型有以下令人费解的行为: import numpy as np # Make a custom dtype with a single triplet of floats (my actual dtype has other # components, but this suffices to demonstrate the problem. dt = np.dtype([('a', np.float64, 3)]) # Make a zero

我发现NumPy和ndarray的自定义数据类型有以下令人费解的行为:

import numpy as np

# Make a custom dtype with a single triplet of floats (my actual dtype has other
# components, but this suffices to demonstrate the problem.
dt = np.dtype([('a', np.float64, 3)])

# Make a zero array with this dtype:
points = np.zeros((4, 4), dtype=dt)

# Try to edit an entry:
points[0][0]['a'] = np.array([1, 1, 1])

print points[0][0]['a']
现在,它不再像我预期的那样包含[1.1.1],而是包含[1.0.0],只在第一个坐标上执行赋值。我可以通过明智地执行赋值坐标来解决这个问题,但是考虑到在这种情况下,完整赋值应该是默认行为,这似乎是不必要的


你有什么想法吗?

如果你改变索引的顺序,像这样:
点['a'][0][0]=np.array([1,1,1])
,它对我来说没问题(在Ubuntu 10.04上是python 2.6.5,numpy 1.3.0)。我希望我知道原因。

如果您希望您的方法有效,有许多方法可以分配点:

points[0][0]['a'][:] = np.array([1, 1, 1])
或:


因为点[0,0]['a']是一个数组,如果要更改数组的内容,应该使用索引。

对我也适用。虽然我真正想做的是:p=points[0][0]p['a']=np.array([1,1,1])和其他必要的对p的操作。@Tim:AFAIK,首先指定命名列,然后再指定数字索引似乎很自然。因此,对我来说,写点['a'][0]或点[0]['a'](适用于数据类型的POD数组)的能力就像是一顿免费的午餐。
points[0,0]['a'][:] = np.array([1, 1, 1])