python-遇到dictionary changed size during iteration

 1 c=0
 2 f={}
 3 jiao={‘脚本一‘:122000,‘脚本二‘:189999,
 4        ‘脚本三‘:99999,‘脚本4‘:25000000,‘jiaoben‘:126}
 5 for i in jiao.values():
 6     c=c+i
 7 average=c/len(jiao)
 8 for li in jiao.keys():
 9     if jiao[li]>average:
10         del jiao[li]
11 # f.setdefault(li,jiao[li])
12 print(average)
13 print(li)

此时运行出现一下错误:

RuntimeError: dictionary changed size during iteration # 字典在迭代的时候改变了字典大小

这个问题在网上找的两种方法:1.是通过转换成列表进行删除;

             2.一下为网上拷贝

for key in result.keys(): # 由 result 变为 result.keys()

    if not result[key]:

        del result[key]

        continue

第二种经过测试并没有解决问题,警告依然存在,

解决办法是不对它进行删除,对不删除的数据重新赋值给一个新的字典.

原文地址:https://www.cnblogs.com/leo-tail-x/p/9409461.html

时间: 2024-08-29 10:00:25

python-遇到dictionary changed size during iteration的相关文章

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.pyTraceback (most recent call last): File "C:/Users/Administrator/PycharmProjects/pythondemo/maptest.py

Python面试题目之(针对dict或者set数据类型)边遍历 边修改 报错dictionary changed size during iteration

# result 是一个字典, 把里面属性值是None的属性删除 for key in result: if not result[key]: del result[key] continue 但是报错信息如下 RuntimeError: dictionary changed size during iteration # 字典在迭代的时候改变了字典大小 python 遍历一个dict.set类型的同时,并且在改变这个变量的长度或者一边遍历一边修改,这时候就会抛出这错误: 我查了一些资料之后, 

python : dictionary changed size during iteration

1. 错误方式 #这里初始化一个dict >>> d = {'a':1, 'b':0, 'c':1, 'd':0} #本意是遍历dict,发现元素的值是0的话,就删掉 >>> for k in d: ... if d[k] == 0: ... del(d[k]) ... Traceback (most recent call last): File "<stdin>", line 1, in <module> RuntimeE

python 报错RuntimeError: dictionary changed size during iteration

1 a = {'1':11, '2':0, '3':0} 2 for b in list(a.keys()): 3 if a[b] == 0: 4 del a[b] 5 6 print(a) 报错是因为在字典迭代期间改变字典大小 我们可以通过取出字典的键值,在转化为列表,这样在for循环期间就可以删除了 原文地址:https://www.cnblogs.com/lonelyshy/p/9775717.html

字典遍历过程中修改字典元素,报错 RuntimeError: dictionary changed size during iteration

https://blog.csdn.net/u013344884/article/details/81867225 原文地址:https://www.cnblogs.com/come202011/p/12432224.html

Python 字典(Dictionary) get()方法

描述 Python 字典(Dictionary) get() 函数返回指定键的值,如果值不在字典中返回默认值. 语法 get()方法语法: dict.get(key, default=None) 参数 key -- 字典中要查找的键. default -- 如果指定键的值不存在时,返回该默认值值. 返回值 返回指定键的值,如果值不在字典中返回默认值None. 实例 以下实例展示了 get()函数的使用方法: #!/usr/bin/python dict = {'Name': 'Zara', 'A

Python 字典(Dictionary) setdefault()方法

描述 Python 字典(Dictionary) setdefault() 函数和get()方法类似, 如果键不已经存在于字典中,将会添加键并将值设为默认值. 语法 setdefault()方法语法: dict.setdefault(key, default=None) 参数 key -- 查找的键值. default -- 键不存在时,设置的默认键值;存在则不设置. 返回值 该方法没有任何返回值. 实例 以下实例展示了 setdefault()函数的使用方法: #!/usr/bin/pytho

Python 字典(Dictionary)day11

字典是另一种可变容器模型,且可存储任意类型对象,如其他容器模型. 字典由键和对应值成对组成.字典也被称作关联数组或哈希表.基本语法如下: dict = {'Alice': '2341', 'Beth': '9102', 'Cecil': '3258'} 也可如此创建字典: dict1 = { 'abc': 456 };dict2 = { 'abc': 123, 98.6: 37 }; 每个键与值用冒号隔开(:),每对用逗号,每对用逗号分割,整体放在花括号中({}). 键必须独一无二,但值则不必.

Python 字典(Dictionary) has_key()方法-判断键是否存在于字典中,如果键在字典dict里返回true,否则返回false

描述 Python 字典(Dictionary) has_key() 函数用于判断键是否存在于字典中,如果键在字典dict里返回true,否则返回false. 语法 has_key()方法语法: dict.has_key(key) 参数 key -- 要在字典中查找的键. 返回值 如果键在字典里返回true,否则返回false. 实例 以下实例展示了 has_key()函数的使用方法: #!/usr/bin/python dict = {'Name': 'Zara', 'Age': 7} pri