码迷,mamicode.com
首页 > 编程语言 > 详细

python错误之RuntimeError: dictionary changed size during iteration

时间:2017-12-13 23:30:46      阅读:232      评论:0      收藏:0      [点我收藏+]

标签:use   dmi   exe   pop   runtime   mod   ogr   mos   har   

pythonn报错信息:
C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\python.exe C:/Users/Administrator/PycharmProjects/pythondemo/maptest.py
Traceback (most recent call last):
File "C:/Users/Administrator/PycharmProjects/pythondemo/maptest.py", line 5, in <module>
for line in maps.keys():
RuntimeError: dictionary changed size during iteration


# python2中实现遍历的同时删除字典中的元素;python3中运行报错信息:
"""
C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\python.exe C:/Users/Administrator/PycharmProjects/pythondemo/maptest.py
Traceback (most recent call last):
File "C:/Users/Administrator/PycharmProjects/pythondemo/maptest.py", line 5, in <module>
for line in maps.keys():
RuntimeError: dictionary changed size during iteration
"""
# maps = {1:"李明",2:"丹尼"}
# for line in maps.keys():
# if(line == 2):
# maps.pop(line)
# print(maps)



# python3中实现遍历的同时删除字典中的元素
maps = {1:"李明",2:"丹尼"}
for line in list(maps.keys()):
if(line == 2):
maps.pop(line)
print(maps)

python错误之RuntimeError: dictionary changed size during iteration

标签:use   dmi   exe   pop   runtime   mod   ogr   mos   har   

原文地址:http://www.cnblogs.com/g177w/p/err1.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!