Python - avoid changing value in a variable

1

Good afternoon

The problem is that the first function changes the value of my list, and the second function takes the modified list as a parameter, and I want the second function to also take the original list as a parameter.

import math
import Queue


Uinicial = raw_input("Ingrese numeros aleatoriamente del 0-8: ")
Einicial = map(int, str(Uinicial))
Efinal = [1,2,3,4,5,6,7,8,0]
posicion_de_cero = Einicial.index(0)
cola = Queue.Queue()


def movimiento_izquierda(posicion_de_cero,Einicial):
    #Movimiento hacia a la Izquierda
    Einicial
    izq_inicial = Einicial
    print izq_inicial
    new_posicion_izquierda = posicion_de_cero - 1
    izq_inicial[posicion_de_cero], izq_inicial[new_posicion_izquierda] = izq_inicial[new_posicion_izquierda], izq_inicial[posicion_de_cero]
    print izq_inicial

def movimiento_derecha(posicion_de_cero,Einicial):
    #Movimiento hacia a la derecha
    der_inicial = Einicial
    print der_inicial
    new_posicion_derecha = posicion_de_cero + 1
    der_inicial[posicion_de_cero], der_inicial[new_posicion_derecha] = der_inicial[new_posicion_derecha], der_inicial[posicion_de_cero]
    print der_inicial

movimiento_izquierda(posicion_de_cero,Einicial)
movimiento_derecha(posicion_de_cero,Einicial)
    
asked by Rasimus.84 21.06.2017 в 21:52
source

2 answers

2

If you do not want your Einicial list to be modified you should make a dead copy of it and not:

izq_inicial = Einicial

With this you only have both variables referencing the same object in memory, therefore modifying one implies modifying the other (they are the same object list ). To create a dead copy (deep copy) use copy.deepcopy :

import copy
import math
import Queue


Uinicial = raw_input("Ingrese numeros aleatoriamente del 0-8: ")
Einicial = map(int, str(Uinicial))
Efinal = [1,2,3,4,5,6,7,8,0]
posicion_de_cero = Einicial.index(0)
cola = Queue.Queue()


def movimiento_izquierda(posicion_de_cero,Einicial):
    #Movimiento hacia a la Izquierda
    Einicial
    izq_inicial = copy.deepcopy(Einicial)
    print izq_inicial
    new_posicion_izquierda = posicion_de_cero - 1
    izq_inicial[posicion_de_cero], izq_inicial[new_posicion_izquierda] = izq_inicial[new_posicion_izquierda], izq_inicial[posicion_de_cero]
    print izq_inicial

def movimiento_derecha(posicion_de_cero,Einicial):
    #Movimiento hacia a la derecha
    der_inicial = copy.deepcopy(Einicial)
    print der_inicial
    new_posicion_derecha = posicion_de_cero + 1
    der_inicial[posicion_de_cero], der_inicial[new_posicion_derecha] = der_inicial[new_posicion_derecha], der_inicial[posicion_de_cero]
    print der_inicial

movimiento_izquierda(posicion_de_cero,Einicial)
movimiento_derecha(posicion_de_cero,Einicial)
    
answered by 21.06.2017 в 22:09
1

The problem is that the lists are passed by reference so that basically your functions are operating on EInicial , you can easily solve it if in each function you perform a copy to a new list, for example: nueva_lista = list(Einicial) , your two functions would be then:

def movimiento_izquierda(posicion_de_cero,Einicial):
    #Movimiento hacia a la IzquierdaEinicial
    nueva_lista = list(Einicial)
    izq_inicial = nueva_lista
    print izq_inicial
    new_posicion_izquierda = posicion_de_cero - 1
    izq_inicial[posicion_de_cero], izq_inicial[new_posicion_izquierda] = izq_inicial[new_posicion_izquierda], izq_inicial[posicion_de_cero]
    print izq_inicial

def movimiento_derecha(posicion_de_cero,Einicial):
    #Movimiento hacia a la derecha
    nueva_lista = list(Einicial)
    der_inicial = nueva_lista
    print der_inicial
    new_posicion_derecha = posicion_de_cero + 1
    der_inicial[posicion_de_cero], der_inicial[new_posicion_derecha] = der_inicial[new_posicion_derecha], der_inicial[posicion_de_cero]
    print der_inicial

Keep in mind that this form of copying would not work as expected if the list has objects, in these cases we have copy.deepcopy() . I recommend reading this link.

    
answered by 21.06.2017 в 22:10