I’m trying to pop off the values I’m appending to the list M each time I’ve evaluated the difference between them and their expected values. I’m printing the result of a list before and after I use M.pop(). Is it using a location in memory and messing up my list inside of deltaL?
M=[]
delta = 3
while abs(delta) > 0.3:
for num1 in range(450,800,20):
best_config_per_numL = []
delta_mL = []
for config in ['FFF','FFS','FSF','FSS','SFF','SFS','SSF','SSS']:
M.append(gen(num1,config[0]))
M.append(gen(num1,config[1]))
M.append(gen(num1,config[2]))
xyL = []
xL = range(400,801,1)
for i in xL:
xyL.append([geny(M,i),i])
deltaL = []
yL = range(400,801,1)
for i in range(len(yL)):
expected = yL[i]
actual = xyL[i][0].real
deltaL.append(abs(expected - actual))
delta_mL.append([max(deltaL),M])
print '\n'+str(delta_mL)+'\n' #<-------------------------- LINE 1
M.pop()
M.pop()
M.pop()
print '\n'+str(delta_mL)+'\n' #<-------------------------- LINE 2
best_config_per_numL.append(delta_mL[0].sort()[0]) #best config for all lambda
M.append(best_config_per_numL.sort()[0][1])
delta = best_config_per_numL.sort()[0][0]
The output of LINE 1 is:
[[1.0, [[1.9736842105263157, 57.0], [1.9736842105263157, 57.0], [1.9736842105263157, 57.0]]]]
[[1.0, [[1.9736842105263157, 57.0], [1.9736842105263157, 57.0], [1.0514018691588785, 107.0]]], [0.99749174811000929, [[1.9736842105263157, 57.0], [1.9736842105263157, 57.0], [1.0514018691588785, 107.0]]]]
[[1.0, [[1.9736842105263157, 57.0], [1.0514018691588785, 107.0], [1.9736842105263157, 57.0]]], [0.99749174811000929, [[1.9736842105263157, 57.0], [1.0514018691588785, 107.0], [1.9736842105263157, 57.0]]], [0.90639755394574695, [[1.9736842105263157, 57.0], [1.0514018691588785, 107.0], [1.9736842105263157, 57.0]]]]
[[1.0, [[1.9736842105263157, 57.0], [1.0514018691588785, 107.0], [1.0514018691588785, 107.0]]], [0.99749174811000929, [[1.9736842105263157, 57.0], [1.0514018691588785, 107.0], [1.0514018691588785, 107.0]]], [0.90639755394574695, [[1.9736842105263157, 57.0], [1.0514018691588785, 107.0], [1.0514018691588785, 107.0]]], [0.78984872616045532, [[1.9736842105263157, 57.0], [1.0514018691588785, 107.0], [1.0514018691588785, 107.0]]]]
[[1.0, [[1.0514018691588785, 107.0], [1.9736842105263157, 57.0], [1.9736842105263157, 57.0]]], [0.99749174811000929, [[1.0514018691588785, 107.0], [1.9736842105263157, 57.0], [1.9736842105263157, 57.0]]], [0.90639755394574695, [[1.0514018691588785, 107.0], [1.9736842105263157, 57.0], [1.9736842105263157, 57.0]]], [0.78984872616045532, [[1.0514018691588785, 107.0], [1.9736842105263157, 57.0], [1.9736842105263157, 57.0]]], [0.99749174811000885, [[1.0514018691588785, 107.0], [1.9736842105263157, 57.0], [1.9736842105263157, 57.0]]]]
[[1.0, [[1.0514018691588785, 107.0], [1.9736842105263157, 57.0], [1.0514018691588785, 107.0]]], [0.99749174811000929, [[1.0514018691588785, 107.0], [1.9736842105263157, 57.0], [1.0514018691588785, 107.0]]], [0.90639755394574695, [[1.0514018691588785, 107.0], [1.9736842105263157, 57.0], [1.0514018691588785, 107.0]]], [0.78984872616045532, [[1.0514018691588785, 107.0], [1.9736842105263157, 57.0], [1.0514018691588785, 107.0]]], [0.99749174811000885, [[1.0514018691588785, 107.0], [1.9736842105263157, 57.0], [1.0514018691588785, 107.0]]], [0.77268527172444679, [[1.0514018691588785, 107.0], [1.9736842105263157, 57.0], [1.0514018691588785, 107.0]]]]
[[1.0, [[1.0514018691588785, 107.0], [1.0514018691588785, 107.0], [1.9736842105263157, 57.0]]], [0.99749174811000929, [[1.0514018691588785, 107.0], [1.0514018691588785, 107.0], [1.9736842105263157, 57.0]]], [0.90639755394574695, [[1.0514018691588785, 107.0], [1.0514018691588785, 107.0], [1.9736842105263157, 57.0]]], [0.78984872616045532, [[1.0514018691588785, 107.0], [1.0514018691588785, 107.0], [1.9736842105263157, 57.0]]], [0.99749174811000885, [[1.0514018691588785, 107.0], [1.0514018691588785, 107.0], [1.9736842105263157, 57.0]]], [0.77268527172444679, [[1.0514018691588785, 107.0], [1.0514018691588785, 107.0], [1.9736842105263157, 57.0]]], [0.78984872616045532, [[1.0514018691588785, 107.0], [1.0514018691588785, 107.0], [1.9736842105263157, 57.0]]]]
[[1.0, [[1.0514018691588785, 107.0], [1.0514018691588785, 107.0], [1.0514018691588785, 107.0]]], [0.99749174811000929, [[1.0514018691588785, 107.0], [1.0514018691588785, 107.0], [1.0514018691588785, 107.0]]], [0.90639755394574695, [[1.0514018691588785, 107.0], [1.0514018691588785, 107.0], [1.0514018691588785, 107.0]]], [0.78984872616045532, [[1.0514018691588785, 107.0], [1.0514018691588785, 107.0], [1.0514018691588785, 107.0]]], [0.99749174811000885, [[1.0514018691588785, 107.0], [1.0514018691588785, 107.0], [1.0514018691588785, 107.0]]], [0.77268527172444679, [[1.0514018691588785, 107.0], [1.0514018691588785, 107.0], [1.0514018691588785, 107.0]]], [0.78984872616045532, [[1.0514018691588785, 107.0], [1.0514018691588785, 107.0], [1.0514018691588785, 107.0]]], [1.0, [[1.0514018691588785, 107.0], [1.0514018691588785, 107.0], [1.0514018691588785, 107.0]]]]
The output of LINE 2 is:
[[1.0, []], [0.99749174811000929, []], [0.90639755394574695, []], [0.78984872616045532, []], [0.99749174811000885, []], [0.77268527172444679, []], [0.78984872616045532, []], [1.0, []]]
I expected to get the same thing as LINE 1.
This line:
…creates a list with two items, the second of which is a reference to the list M. When you later change M, you can see the changes in delta_mL because it references the same list. If you want to make a copy of M, try:
This will create a new list that contains the same items as M, but that is a separate copy so that it won’t change when you add or remove items in M.
Remember that in Python, variables store references to objects. If you want to make a copy of an object, you need to do so explicitly. This is, of course, only important when the objects in question are mutable, like lists are. Immutable objects – such as numbers, tuples and strings – aren’t a problem, because even if many variables store references to the same immutable object, none of them can modify it, so sharing is not a problem.