zoukankan      html  css  js  c++  java
  • 循环字典进行操作时出现:RuntimeError: dictionary changed size during iteration的解决方案

    在做对员工信息增删改查这个作业时,有一个需求是通过用户输入的id删除用户信息。我把用户信息从文件提取出来储存在了字典里,其中key是用户id,value是用户的其他信息。在循环字典的时候,当用户id和字典里的key相等时,会删除这条信息,当时删除时报错RuntimeError: dictionary changed size during iteration。

    for key in staff_info:
        if user_id == key:
            print(key)
            staff_info.pop(key)
            id_exist = True

    参考:https://www.python.org/dev/peps/pep-0234/#dictionary-iterators

    在官网看到解释:

    Dictionaries implement a tp_iter slot that returns an efficient iterator that iterates over the keys of the dictionary. During such an iteration, the dictionary should not be modified, except that setting the value for an existing key is allowed (deletions or additions are not, nor is the update() method). This means that we can write

    for k in dict: ...
    

    which is equivalent to, but much faster than

    for k in dict.keys(): ...
    

    as long as the restriction on modifications to the dictionary (either by the loop or by another thread) are not violated.

    意思是多字典在被循环的时候不能被循环删除和更新,除了给一个已经存在的key设置value。还说道 for kin dict: ...和for k in dict.keys(): ...效果是一样的,但是前者速度更快。

    那就来试验一下:

    a = {'a': 1, 'b': 0, 'c': 1, 'd': 0}
    
    for key in a:
        print(key, a[key])
    
    for key in a.keys():
        print(key, a[key])
    
    # 两个循环效果一样
    
    print(a.keys())  # dict_keys(['a', 'b', 'c', 'd'])
    print(a)  # {'a': 1, 'b': 0, 'c': 1, 'd': 0}

    除了a.keys是dict_keys类型,它们的效果是一样的。

    那么解决方案就出来了,转换成列表形式就OK了。

    for key in list(staff_info.keys()):  
        if user_id == key:
            print(key)
            staff_info.pop(key)
            id_exist = True
    有道词典
    for k in dict: ...
    详细X
    k的dict类型:…

  • 相关阅读:
    js文件内部导入引用js文件方法
    CSS3.0动画之hover---Y轴----3D旋转
    静态的html页面想要设置使用浏览器缓存
    opener 属性是一个可读可写的属性,可返回对创建该窗口的 Window 对象的引用
    input获取永久焦点
    修改zepto源代码,使支持wp8的ie10
    EChart
    input属性disabled和readonly的区别
    trigger
    解决jquery mobile的遇到高版本Chrome一直转圈,页面加载不出来的情况。
  • 原文地址:https://www.cnblogs.com/lshedward/p/9988640.html
Copyright © 2011-2022 走看看