简体   繁体   English

numpy,将新值分配给现有的ndarray

[英]Numpy, assign new values to a existing ndarray

I'm new to NumPy, a problem blocks me.. --- I want to change a ndarray's value: 我是NumPy的新手,一个问题阻止了我。--我想更改ndarray的值:

Here is the debug info. 这是调试信息。

(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)

Why doesn't the value of Node[0,0]['f'] change? 为什么Node[0,0]['f']的值没有变化?

Try using Nodes['f'][0,0] = numpy.array([i/9.0 for i in range(9)]) instead: 尝试使用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]

I'm not sure why there's a difference, but it is probably related to this question . 我不确定为什么会有区别,但这可能与此问题有关

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM