0

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.

6
  • What exactly are you asking? I assume the root cause is yet another misunderstanding of what Python's varibles are. But I can make little sense of what exact problem you're having. Commented Feb 9, 2011 at 15:09
  • Please don't print "str(something)". It looks confusing and does nothing.. Please replace all of your print...str(...)... with simpler print "\n", max(deltaL), M, "\n" Commented Feb 9, 2011 at 15:10
  • Also, the code is not correctly indented, "LINE 2" has 7 leading spaces. In my answer I supposed that they were 8. Commented Feb 9, 2011 at 15:12
  • As I told you, you cannot get the same value that you have in LINE 1 because LINE 2 is in a different part of the program, where the M list is empty. Commented Feb 9, 2011 at 15:17
  • I've updated the question. The print line I was showing previously was an obvious answer. The new print line error is more difficult. Commented Feb 9, 2011 at 15:21

2 Answers 2

4

This line:

delta_mL.append([max(deltaL),M])

...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:

delta_mL.append([max(deltaL),list(M)])

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.

Sign up to request clarification or add additional context in comments.

Comments

0

Is it using a location in memory and messing up my list inside of deltaL?

In a way.

delta_mL.append([max(deltaL),M])

delta_mL has a reference to M. Not a copy of M. If M changes, so does delta_mL.

You might not want to simply append M. You might want to use M[:] to make a copy of M.

1 Comment

Updated the post to more accurately reflect my question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.