Python List Variable - Is it using the memory location or something? -


i'm trying pop off values i'm appending list m each time i've evaluated difference between them , expected values. i'm printing result of list before , after use m.pop(). using location in memory , messing list inside of deltal?

m=[] delta = 3 while abs(delta) > 0.3:     num1 in range(450,800,20):         best_config_per_numl = []         delta_ml = []         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)             in xl:                 xyl.append([geny(m,i),i])              deltal = []             yl = range(400,801,1)             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 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 same thing line 1.

this line:

delta_ml.append([max(deltal),m]) 

...creates list 2 items, second of reference list m. when later change m, can see changes in delta_ml because references same list. if want make copy of m, try:

delta_ml.append([max(deltal),list(m)]) 

this create new list contains same items m, separate copy won't change when add or remove items in m.

remember in python, variables store references objects. if want make copy of object, need explicitly. is, of course, important when objects in question mutable, lists are. immutable objects - such numbers, tuples , strings - aren't problem, because if many variables store references same immutable object, none of them can modify it, sharing not problem.


Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -