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



上一篇:pandas中计算总体标准差
下一篇:Dataframe的总行数和总列数大小多少汇总统计shape,len,index,count

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