python错误之RuntimeError: dictionary changed size during iteration

Posted 海的味道

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python错误之RuntimeError: dictionary changed size during iteration相关的知识,希望对你有一定的参考价值。

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的主要内容,如果未能解决你的问题,请参考以下文章

RuntimeError:python 多处理错误

Python zipfile:RuntimeError:文件密码错误

python和PyQt5中的Quamash QventLoop“RuntimeError:没有运行事件循环”错误

PyQt4 python中的“RuntimeError:调用Python对象时超出最大递归深度”错误

RuntimeError: Python is not installed as a framework 错误解决办法

Python Asyncio 错误:“OSError:[WinError 6] 句柄无效”和“RuntimeError:事件循环已关闭”[重复]