如何使用python中的循环为ndarray变量赋值

时间:2014-12-01 17:11:38

标签: python python-2.7 numpy multidimensional-array

dtype 定义为对象的ndarray a并用词典填充

a = np.ndarray(shape=(2,3,4), dtype=object)
a.fill({'position':(0,0,0)})

这是a

的值
array([[[{'position': (0, 0, 0)}, {'position': (0, 0, 0)},
     {'position': (0, 0, 0)}, {'position': (0, 0, 0)}],
    [{'position': (0, 0, 0)}, {'position': (0, 0, 0)},
     {'position': (0, 0, 0)}, {'position': (0, 0, 0)}],
    [{'position': (0, 0, 0)}, {'position': (0, 0, 0)},
     {'position': (0, 0, 0)}, {'position': (0, 0, 0)}]],

   [[{'position': (0, 0, 0)}, {'position': (0, 0, 0)},
     {'position': (0, 0, 0)}, {'position': (0, 0, 0)}],
    [{'position': (0, 0, 0)}, {'position': (0, 0, 0)},
     {'position': (0, 0, 0)}, {'position': (0, 0, 0)}],
    [{'position': (0, 0, 0)}, {'position': (0, 0, 0)},
     {'position': (0, 0, 0)}, {'position': (0, 0, 0)}]]], dtype=object)

我想将词典中的每个元组替换为元组(X-index,Y-index,Z-index)。我尝试numpy.ndenumerate循环来分配值,如下面的代码

for (x_index, y_index, z_index), temp in np.ndenumerate(a):
    a[x_index][y_index][z_index]['position'] = (x_index, y_index, z_index)

每个元组都会被分配到最新值(1,2,3)a的值为

array([[[{'position': (1, 2, 3)}, {'position': (1, 2, 3)},
     {'position': (1, 2, 3)}, {'position': (1, 2, 3)}],
    [{'position': (1, 2, 3)}, {'position': (1, 2, 3)},
     {'position': (1, 2, 3)}, {'position': (1, 2, 3)}],
    [{'position': (1, 2, 3)}, {'position': (1, 2, 3)},
     {'position': (1, 2, 3)}, {'position': (1, 2, 3)}]],

   [[{'position': (1, 2, 3)}, {'position': (1, 2, 3)},
     {'position': (1, 2, 3)}, {'position': (1, 2, 3)}],
    [{'position': (1, 2, 3)}, {'position': (1, 2, 3)},
     {'position': (1, 2, 3)}, {'position': (1, 2, 3)}],
    [{'position': (1, 2, 3)}, {'position': (1, 2, 3)},
     {'position': (1, 2, 3)}, {'position': (1, 2, 3)}]]], dtype=object)

如何获得所需的输出

array([[[{'position': (0, 0, 0)}, {'position': (0, 0, 1)},
     {'position': (0, 0, 2)}, {'position': (0, 0, 3)}],
    [{'position': (0, 1, 0)}, {'position': (0, 1, 1)},
     {'position': (0, 1, 2)}, {'position': (0, 1, 3)}],
    [{'position': (0, 2, 0)}, {'position': (0, 2, 1)},
     {'position': (0, 2, 2)}, {'position': (0, 2, 3)}]],

   [[{'position': (1, 0, 0)}, {'position': (1, 0, 1)},
     {'position': (1, 0, 2)}, {'position': (1, 0, 3)}],
    [{'position': (1, 1, 0)}, {'position': (1, 1, 1)},
     {'position': (1, 1, 2)}, {'position': (1, 1, 3)}],
    [{'position': (1, 2, 0)}, {'position': (1, 2, 1)},
     {'position': (1, 2, 2)}, {'position': (1, 2, 3)}]]], dtype=object)

1 个答案:

答案 0 :(得分:0)

这是一个copy by reference问题。您更新一个字典,其他每个字体都会使用相同的值进行更新。这就是为什么每个值都是{'position': (1, 2, 3)},因为它是上次更新。要解决此问题,请每次创建新词典

>>> a = np.ndarray(shape=(2,3,4), dtype=object)
>>> for (x_index, y_index, z_index), temp in np.ndenumerate(a):
...     a[x_index][y_index][z_index] = {'position':(x_index, y_index, z_index)}
... 
>>> a
array([[[{'position': (0, 0, 0)}, {'position': (0, 0, 1)},
         {'position': (0, 0, 2)}, {'position': (0, 0, 3)}],
        [{'position': (0, 1, 0)}, {'position': (0, 1, 1)},
         {'position': (0, 1, 2)}, {'position': (0, 1, 3)}],
        [{'position': (0, 2, 0)}, {'position': (0, 2, 1)},
         {'position': (0, 2, 2)}, {'position': (0, 2, 3)}]],

       [[{'position': (1, 0, 0)}, {'position': (1, 0, 1)},
         {'position': (1, 0, 2)}, {'position': (1, 0, 3)}],
        [{'position': (1, 1, 0)}, {'position': (1, 1, 1)},
         {'position': (1, 1, 2)}, {'position': (1, 1, 3)}],
        [{'position': (1, 2, 0)}, {'position': (1, 2, 1)},
         {'position': (1, 2, 2)}, {'position': (1, 2, 3)}]]], dtype=object)