我对dict的方法-tab_bin()有问题,我在代码中复制了"tab_bin“- "HERE 02”,然后我修改了它- "HERE 01“。当它在代码末尾打印"tab_bin“时,它会打印"tab_bin”的修改版本。我也知道我的代码有点乱,变量的名字看起来很奇怪,因为它们是用波兰语写的。我的代码:
def setting(akcje,n,m):
def zmiana():
Zm_1 = []
Zm_2 = []
for x in range(akcje[1], akcje[3]+1):
Zm_1.append(x)
for y in range(akcje[0], akcje[2]+1):
Zm_2.append(y)
return Zm_1, Zm_2
def kreator_tab():
if bool(tab_bin) == False:
for x in range(1, n+1):
for y in range(m):
tab_bin.setdefault(x, []).append(0)
for key, value in tab_bin.items():
for x in Zm_2:
if key == x:
for y in Zm_1:
if value[y-1] == 0: value[y-1] = 1
else: value[y-1] = 0
def operacje_proste(akcje, liczba_operacji):
def kreator_zmian():
ZmX = []
ZmY = []
for x in range(1, akcje[1]+1):
ZmX.append(x)
for y in range(1, akcje[0]+1):
ZmY.append(y)
return ZmX, ZmY
def kreator_tab_prostych():
for keys, values in tab_operacyjna.items(): # <- HERE 01
for x in ZmY:
if keys == x:
for y in ZmX:
if values[y-1] == 0: values[y-1] = 1
else: values[y-1] = 0
return liczba_operacji+1
def d():
suma =0
for value in tab_operacyjna.values():
for x in value:
suma+=x
if suma == 0: return print(liczba_operacji)
else:
return operacje_proste([max(ZmY),max(ZmX)-1], liczba_operacji)
ZmX,ZmY= kreator_zmian()
liczba_operacji = kreator_tab_prostych()
d()
Zm_1, Zm_2 = zmiana()
kreator_tab()
tab_operacyjna = tab_bin.copy() # <- HERE 02
operacje_proste([max(Zm_2),max(Zm_1)], 0)
def wrapper():
n = 2
m = 3
q = 3
akcje = [1,2,2,2]
setting(akcje,n,m)
print(tab_bin)
wrapper()编辑:好的,为了更清楚地解释我的意思,我将在代码中添加print()并显示输出。代码:
tab_operacyjna = tab_bin.copy()
print(tab_bin, "- tab_bin ",tab_operacyjna,"- tab_operacyjna")
operacje_proste([max(Zm_2),max(Zm_1)], 0)
print(tab_bin, "- tab_bin ",tab_operacyjna,"- tab_operacyjna")OutPut:{1: 0,1,0,2: 0,1,0} - tab_bin {1: 0,1,0,2: 0,1,0} - tab_operacyjna
{1: 0,0,0,2: 0,0,0} - tab_bin {1: 0,0,0,2: 0,0,0} - tab_operacyjna
即使在operacje_proste()中我不使用tab_bin,它也会像我在tab_operacyja上进行更改一样进行更改。我希望这次编辑能让它变得更清楚。
发布于 2021-01-28 00:44:43
正如juanpa.arrivillaga所写的,我认为.copy的工作方式与.deepcopy()类似。对于任何有类似问题的人,只需查看this。
https://stackoverflow.com/questions/65919624
复制相似问题