Variable Changes Without Being Touched
I'm quite new to Python but this is strange. So, I have a matrix (a list of lists) and I pass it to a function (creaMatrixVNS). def creaMatrixVNS(best_matrice, time): matrice
Solution 1:
When you do matrice = best_matrice
, you're just creating a new reference named matrice
pointing to the same object as the reference best_matrice
.
Check out the accepted answer for Deep copy a list in Python for an example of a deep copy of a list in Python.
Solution 2:
A quick shorthand for copying lists 'deeply' without importing is the use of the :
slice operator. In your situation, you'd need to use it twice to explicitly duplicate the lists and their contents rather than copying references to the inner lists.
A trivial example is new_lst = old_lst[:]
to copy all contents of old_lst
-- in your case, you'd want to do matrice = best_matrice[:][:]
, thereby copying the contents of the inner lists, too.
Post a Comment for "Variable Changes Without Being Touched"