以下是我的代碼,我打算計算列表多少次修改,但數量似乎是錯的,我的代碼如下所示:爲什麼目標計數與預期不同?
t=(0,999999,"state")
cachLine = []
for x in range(0,2):
cachLine.append(t);
cache = []
for x in range(0,1):
cache.append(cachLine)
cacheList = []
for x in range(0,8):
cacheList.append(cache)
count=0
for cacheI,cache in enumerate(cacheList):
for clI,cl in enumerate(cache):
for bI,(valid, address, state) in enumerate(cl):
if state =='state':
cacheList[cacheI][clI][bI] = (valid, address,'invalid')
count +=1
print(count)
計數在這種情況下,2,未如預期,它應爲16
然而,如果我改變計數的順序+ = 1至
for cacheI,cache in enumerate(cacheList):
for clI,cl in enumerate(cache):
for bI,(valid, address, state) in enumerate(cl):
count +=1
if state =='state':
cacheList[cacheI][clI][bI] = (valid, address,'invalid')
print(count)
我得到的16正確的計數。在任何兩個殼體的,我得到cacheList的輸出:
[[[(0, 999999, 'invalid'), (0, 999999, 'invalid')]], [[(0, 999999, 'invalid'), (0, 999999, 'invalid')]], [[(0, 999999, 'invalid'), (0, 999999, 'invalid')]], [[(0, 999999, 'invalid'), (0, 999999, 'invalid')]], [[(0, 999999, 'invalid'), (0, 999999, 'invalid')]], [[(0, 999999, 'invalid'), (0, 999999, 'invalid')]], [[(0, 999999, 'invalid'), (0, 999999, 'invalid')]], [[(0, 999999, 'invalid'), (0, 999999, 'invalid')]]]
它是元組中的16個元素(0,9999999,'invalid')。
第一個代碼中的計數有什麼問題?