G
ghw
What am I missing? I am using Python 3.1.2.
ff = [[0.0]*5]*5
ff #(lists 5x5 array of 0.0)
for i in range(5):
for j in range(3):
ff[j] = i*10+j
print (i,j,ff[j]) # correctly prints ff array values
ff # try this and see what happens!
result of first print of ff
[[0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0, 0.0]]
result of second print of ff
0 0 0
0 1 1
0 2 2
1 0 10
1 1 11
1 2 12
2 0 20
2 1 21
2 2 22
3 0 30
3 1 31
3 2 32
4 0 40
4 1 41
4 2 42
result of third print of ff
[[40, 41, 42, 0.0, 0.0], [40, 41, 42, 0.0, 0.0],
[40, 41, 42, 0.0, 0.0], [40, 41, 42, 0.0, 0.0],
[40, 41, 42, 0.0, 0.0]]
Obviously there is something missing in my understanding of
how array values are populated. Why do all "rows" get
populated with the same set of values (40, 41, 42)?
Thanks for your help,
Hamilton Woods
ff = [[0.0]*5]*5
ff #(lists 5x5 array of 0.0)
for i in range(5):
for j in range(3):
ff[j] = i*10+j
print (i,j,ff[j]) # correctly prints ff array values
ff # try this and see what happens!
result of first print of ff
[[0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0, 0.0]]
result of second print of ff
0 0 0
0 1 1
0 2 2
1 0 10
1 1 11
1 2 12
2 0 20
2 1 21
2 2 22
3 0 30
3 1 31
3 2 32
4 0 40
4 1 41
4 2 42
result of third print of ff
[[40, 41, 42, 0.0, 0.0], [40, 41, 42, 0.0, 0.0],
[40, 41, 42, 0.0, 0.0], [40, 41, 42, 0.0, 0.0],
[40, 41, 42, 0.0, 0.0]]
Obviously there is something missing in my understanding of
how array values are populated. Why do all "rows" get
populated with the same set of values (40, 41, 42)?
Thanks for your help,
Hamilton Woods