我嘗試了一些事情列出了交互式解釋,我注意到了這一點:列表「怪癖」
>>> list = range(1, 11)
>>> for i in list:
... list.remove(i)
...
>>> list
[2, 4, 6, 8, 10]
任何人都可以解釋爲什麼它留下偶數?這讓我很困惑......非常感謝。
我嘗試了一些事情列出了交互式解釋,我注意到了這一點:列表「怪癖」
>>> list = range(1, 11)
>>> for i in list:
... list.remove(i)
...
>>> list
[2, 4, 6, 8, 10]
任何人都可以解釋爲什麼它留下偶數?這讓我很困惑......非常感謝。
我覺得這是最簡單的用Python解釋:
>>> for iteration, i in enumerate(lst):
... print 'Begin iteration', iteration, 'where lst =', str(lst), 'and the value at index', iteration, 'is', lst[iteration]
... lst.remove(i)
... print 'End iteration', iteration, 'where lst =', str(lst), 'with', i, 'removed\n'
...
Begin iteration 0 where lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] and the value at index 0 is 1
End iteration 0 where lst = [2, 3, 4, 5, 6, 7, 8, 9, 10] with 1 removed
Begin iteration 1 where lst = [2, 3, 4, 5, 6, 7, 8, 9, 10] and the value at index 1 is 3
End iteration 1 where lst = [2, 4, 5, 6, 7, 8, 9, 10] with 3 removed
Begin iteration 2 where lst = [2, 4, 5, 6, 7, 8, 9, 10] and the value at index 2 is 5
End iteration 2 where lst = [2, 4, 6, 7, 8, 9, 10] with 5 removed
Begin iteration 3 where lst = [2, 4, 6, 7, 8, 9, 10] and the value at index 3 is 7
End iteration 3 where lst = [2, 4, 6, 8, 9, 10] with 7 removed
Begin iteration 4 where lst = [2, 4, 6, 8, 9, 10] and the value at index 4 is 9
End iteration 4 where lst = [2, 4, 6, 8, 10] with 9 removed
請注意,這是一個壞想法:(a)在遍歷它時修改list
;(b)調用list
「列表」。
對於列表[:]中的列表[或列表中的列表],應該解決您的問題,因爲第一個將創建片副本,或者第二個將創建新的不可變對象(元組)。 –
標準警告:將列表命名爲「list」是個不好的習慣,因爲這會破壞內置類型列表。 – DSM
好點! :)當我將它命名爲「list」時,我沒有想到它。 (可能與作者命名他的「列表」) –