1 a = {'1':11, '2':0, '3':0}
2 for b in list(a.keys()):
3 if a[b] == 0:
4 del a[b]
5
6 print(a)
報錯是因為在字典疊代期間改變字典大小
我們可以通過取出字典的鍵值,在轉化為清單,這樣在for循環期間就可以删除了
轉載于:https://www.cnblogs.com/lonelyshy/p/9775717.html
1 a = {'1':11, '2':0, '3':0}
2 for b in list(a.keys()):
3 if a[b] == 0:
4 del a[b]
5
6 print(a)
報錯是因為在字典疊代期間改變字典大小
我們可以通過取出字典的鍵值,在轉化為清單,這樣在for循環期間就可以删除了
轉載于:https://www.cnblogs.com/lonelyshy/p/9775717.html