Fred
Fred

Reputation: 41

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:

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?

Upvotes: 0

Views: 550

Answers (1)

Kallista Bonawitz
Kallista Bonawitz

Reputation: 11

Try using Nodes['f'][0,0] = numpy.array([i/9.0 for i in range(9)]) instead:

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.

Upvotes: 1

Related Questions