1
我有一個列表撤消和另一個ap,我想保存每次我修改ap列表時複製撤消,但當我保存,例如在撤消[2]我已經做了ap ,Undo [1]與Undo [2]相同,它看起來像指定了列表ap而不是ap的值。我發現一個類似的問題,並試圖分配ap [:],所以它會做一個副本,但它沒有。Python列表按值賦值
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
我也試着用append和Undo = Undo + ap [:],你可以看到。 對不起,如果代碼有點混亂。 編輯: 我刪除了最後的編輯,我想更清楚 所以我再拍一段代碼,以恢復我想說
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)
輸出什麼:
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': {}}]]
爲什麼撤銷[0]修改爲撤消[1]? 我試着用Undo.append(list(ap))和Undo = Undo + list(ap)和ap [:]一樣,現在我試着初始化Undo和相同的結果。
能否請你告訴你要如何'Undo'加入到照顧一個例子'ap'和現在看起來如何? – elethan
好的我編輯了帖子 – user7006931