我在這段代碼片段中遇到了一個問題:
temp = matrixInput.copy()
for iy in range(len(temp)):
for runde in range(1, 5):
for ix in temp[iy]:
print("iy:", iy, "ix:", ix, "runde", runde, "len:", len(temp[iy]))
newnumber = ix runde
if newnumber > 9:
newnumber -= 9
matrixInput[iy].append(newnumber)
matrixInput 是一個包含一大堆整數的串列(如果需要,矩陣)。
這個想法是創建一個副本,迭代副本并附加到原始副本。這樣做 4 次,每次附加數字中的值都應增加 1。目標是獲得一個 5 倍于初始值的矩陣。
到現在為止還挺好。但是由于某種原因,當我附加到matrixInput最內層的回圈時會卡住并無休止地附加。temp[iy]盡管我附加到matrixInput[iy]. 我的錯誤是什么?
uj5u.com熱心網友回復:
正如評論中提到的 list.copy() 是淺的,一個解決方案是使用復制庫中的 deepcopy 方法
import copy
...
temp=copy.deepcopy(matrixInput)
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/382206.html
上一篇:Mergeoflist問題:在一定條件下合并二維陣列
下一篇:即使我正在檢查串列值也被覆寫
