zoukankan      html  css  js  c++  java
  • 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)
    

      

  • 相关阅读:
    java中的静态变量与实例变量
    Java中的关键字this
    继承和多类的基础(C++)
    11-1:(42)接雨水
    10-2
    10-1
    9-2
    9-1
    8-2
    8-1
  • 原文地址:https://www.cnblogs.com/g177w/p/err1.html
Copyright © 2011-2022 走看看