One - One Code All

Blog Content

Python字典dictionary changed size during iteration

Python   2016-05-15 17:30:13

Python字典dictionary changed size during iteration

原因:字典一边遍历一边修改

>>> a = {1:0, 2:0, 3:1, 4:1}
>>> for i in a:
...     a.pop(i)
... 
0
Traceback (most recent call last):
  File "", line 1, in 
RuntimeError: dictionary changed size during iteration
>>> for i in a.keys():
...     a.pop(i)
... 
0
Traceback (most recent call last):
  File "", line 1, in 
RuntimeError: dictionary changed size during iteration
>>> for i in list(a.keys()):
...     a.pop(i)
... 
1
1



上一篇:postgresql忘记登录密码 修改密码
下一篇:证券从业资格考试改革前后说明

The minute you think of giving up, think of the reason why you held on so long.