Python列表变量 - 是使用内存位置还是其他东西?

时间:2011-02-09 15:02:42

标签: python

每次我评估它们与预期值之间的差异时,我都会尝试弹出我附加到列表M的值。我在使用M.pop()之前和之后打印列表的结果。它是否在内存中使用了一个位置并弄乱了我在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]

LINE 1的输出为: [[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]]]]

LINE 2的输出为: [[1.0,[]],[0.99749174811000929,[]],[0.90639755394574695,[]],[0.78984872616045532,[]],[0.99749174811000885,[]],[0.77268527172444679,[]],[0.78984872616045532,[]], [1.0,[]]]

我期望得到与LINE 1相同的东西。

2 个答案:

答案 0 :(得分:4)

这一行:

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

...创建一个包含两个项目的列表,第二个是列表M的引用。当您稍后更改M时,您可以看到delta_mL中的更改,因为它引用了相同的名单。如果您想制作M的副本,请尝试:

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

这将创建一个包含与M相同的项目的新列表,但这是一个单独的副本,以便在M中添加或删除项目时不会更改。

请记住,在Python中,变量将引用存储到对象中。如果要复制对象,则需要明确地执行此操作。当然,这只有当有问题的对象是 mutable 时才有用,就像列表一样。不可变对象 - 例如数字,元组和字符串 - 不是问题,因为即使许多变量存储对同一个不可变对象的引用,它们都不能修改它,因此共享不是问题。

答案 1 :(得分:0)

  

是否在内存中使用了一个位置并弄乱了我在deltaL中的列表?

在某种程度上。

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

delta_mLM参考。不是M的副本。如果M发生变化,delta_mL也会发生变化。

您可能不想简单地追加M。您可能希望使用M[:]复制M