Numpy,为现有的ndarray分配新值

时间:2012-03-11 14:48:17

标签: python numpy

我是NumPy的新手,问题阻碍了我...... ---我想改变一个ndarray的价值:

这是调试信息。

(Pdb) Nodes[0,0]['f'] = np.array([i/9.0 for i in  range(9)])
(Pdb) print Nodes[0,0]['f']
[  0.00000000e+00   0.00000000e+00   5.67382835e+10   4.58280650e-41
   1.00030523e-36   0.00000000e+00   1.00030523e-36   0.00000000e+00
   2.28153811e-40]
(Pdb)

为什么Node[0,0]['f']的值不会改变?

1 个答案:

答案 0 :(得分:1)

请尝试使用Nodes['f'][0,0] = numpy.array([i/9.0 for i in range(9)])

import numpy
Nodes = numpy.ndarray(shape=(1,1), dtype=[('f', (float, 9))])
print Nodes[0,0]['f']
# [  0.00000000e+000   2.10042365e-316   2.44222340e-316   6.90749588e-310
#    2.10041417e-316   4.22653002e-317   2.76341350e-316   6.90749588e-310
#    3.95252517e-322]
Nodes[0,0]['f'] = numpy.array([i/9.0 for i in range(9)])
print Nodes[0,0]['f']
# [  0.00000000e+000   2.10042365e-316   2.44222340e-316   6.90749588e-310
#    2.10041417e-316   4.22653002e-317   2.76341350e-316   6.90749588e-310
#    3.95252517e-322]
Nodes['f'][0,0] = numpy.array([i/9.0 for i in range(9)])
print Nodes[0,0]['f']
# [ 0.          0.11111111  0.22222222  0.33333333  0.44444444  0.55555556
#   0.66666667  0.77777778  0.88888889]

我不确定为什么会有区别,但可能与this question有关。