I want to store the intermediate values of a variable in Python. This variable is updated in a loop. When I try to do this with a list.append command, it updates every value in the list with the new value of the variable. How should I do it?
while (step < maxstep):
for i in range(100):
x = a*b*c
f1 += x
f2.append(f1)
print f2
raw_input('<<')
step += 1
Expected output
[array([-2.03,-4.13])]
<<
[array([-2.03,-4.13]),array([-3.14,-5.34])]
Printed output
[array([-2.03,-4.13])]
<<
[array([-3.14,-5.34]),array([-3.14,-5.34])]
Is there a different way of getting what I want in Python?
Assuming the original had a typo and
f1is actuallyfi(or vice versa):fiis a pointer to an object, so you keep appending the same pointer. When you usefi += x, you are actually changing the value of the object to whichfipoints.To solve the issue you can use
fi = fi + xinstead.