Reputation: 19
I have a list Undo and another ap, I want to save every time I modify the ap list a copy on Undo but when I save, for example on Undo[2] what I have done to ap, Undo[1] is the same as Undo[2], it looks like it assign the list ap and not the value of ap. I find a question similar and tried to assign ap[:] so it will make a copy but it didn't.
from Functionalitati import *
from Ui import *
from GlobalVr import *
Active = True
NumarApartamente = int(input("Numarul apartamentelor:"))
cntA = 0
while cntA < NumarApartamente:
ap.append({"gaz":{}, "apa":{}, "electricitate":{}, "canalizare":{}, "altele":{}})
cntA = cntA + 1
Undo = Undo + ap[ : ]
while Active:
while True:
op1 = Umenu()
if op1 > 6 and op1 < 0:
print(x,'Nu este o comanda valida')
else:
break
if op1 == 0:
break
op2 = Submenu(op1)
if op1 == 6 and UndoCont > 0:
UndoCont = UndoCont - 1
Undo.pop()
ap = Undo[UndoCont]
print (Undo)
elif op1 == 1 and op2 == 1 :
ap = AddCheltuiala(ap)
Undo = Undo + ap[ : ]
UndoCont = UndoCont + 1
elif op1 == 1 and op2 == 2 :
ap = ModCheltuiala(ap)
Undo.append(ap[0:NumarApartamente - 1])
UndoCont = UndoCont + 1
elif op1 == 2 and op2 == 1 :
ap = DelCheltuiala(ap)
Undo.append(ap[0:NumarApartamente - 1])
UndoCont = UndoCont + 1
elif op1 == 2 and op2 == 2 :
ap = DelCCheltuiala(ap)
Undo.append(ap[0:NumarApartamente - 1])
UndoCont = UndoCont + 1
elif op1 == 2 and op2 == 3 :
ap = DelTip(ap,NumarApartamente)
Undo.append(ap[0:NumarApartamente - 1])
UndoCont = UndoCont + 1
I also tried with append and Undo = Undo + ap[:] as you can see. Sorry if the code is a little messy. edit: I deleted last edit and I want to make it more clear So I make another piece of code to resume what I want to say
from Functionalitati import *
from Ui import *
import datetime
ap = [] #lista principala
UndoCont = 0
nrAp = int(input("Da-ti numarul apartamentelor:"))
Undo = [None] * nrAp
contor = 0
while contor < nrAp:
ap.append({"gaz":{}, "apa":{}, "electricitate":{}, "canalizare":{}, "altele":{}})
contor = contor + 1
Undo[0] = ap[:]
print(Undo)
print("space" * 4)
ap[1]["gaz"]["date"] = 100
Undo[1] = ap[:]
print (Undo)
the output:
Da-ti numarul apartamentelor:2
[[{'gaz': {}, 'canalizare': {}, 'electricitate': {}, 'altele': {}, 'apa': {}}, {'gaz': {}, 'canalizare': {}, 'electricitate': {}, 'altele': {}, 'apa': {}}], None]
spacespacespacespace
[[{'gaz': {}, 'canalizare': {}, 'electricitate': {}, 'altele': {}, 'apa': {}}, {'gaz': {'date': 100}, 'canalizare': {}, 'electricitate': {}, 'altele': {}, 'apa': {}}], [{'gaz': {}, 'canalizare': {}, 'electricitate': {}, 'altele': {}, 'apa': {}}, {'gaz': {'date': 100}, 'canalizare': {}, 'electricitate': {}, 'altele': {}, 'apa': {}}]]
why the Undo[0] modify as Undo[1] ? I tried with Undo.append(list(ap)) and Undo = Undo + list(ap) and same with ap[:] and now I tried to initialize Undo before and same result.
Upvotes: 1
Views: 69
Reputation: 19
Solved.I don't really know how it works but I use copy.deepcopy()
(copy.copy()
didn't work)
Upvotes: 1