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

时间: 2024-08-16 07:26:13

python 报错RuntimeError: dictionary changed size during iteration的相关文章

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

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

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-遇到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 prin

Django 使用allauth报错 RuntimeError: Model class django.contrib.sites.models.Site doesn&#39;t declare an explicit app_label and isn&#39;t in an application in INSTALLED_APPS

一:报错 RuntimeError: Model class django.contrib.sites.models.Site doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS 出现这种情况需将Django的Sites框架添加到您的应用程序中,并在您的设置中将SITE_ID设置为1即可.位置放在默认配置的后面,其他应用的前面. INSTALLED_APPS = [ 'django

Python报错:SyntaxError: Non-ASCII character &#39;\xe5&#39; in file的解决方法

SyntaxError: Non-ASCII character '\xe5' in file 原因:Python默认是以ASCII作为编码方式的,如果在自己的Python源码中包含了中文(或者其他的语言,比如小日本的日语……),此时即使你把自己编写的Python源文件以UTF-8格式保存了:但实际上,这依然是不行的. 解决方法:在源码的第一行添加以下语句: # -*- coding: UTF-8 -*-     或者 #coding=utf-8 (注:此语句一定要添加在源代码的第一行) Pyt

python报错 IndentationError: unindent does not match any outer indentation level

这个是缩进问题,重新将行前面的空格删掉,换位tab python报错 IndentationError: unindent does not match any outer indentation level,布布扣,bubuko.com python报错 IndentationError: unindent does not match any outer indentation level

关于Python报错:SyntaxError: Non-ASCII character &#39;\xe5&#39; in file的解决方法

Python默认编码错误SyntaxError: Non-ASCII character '\xe5'之解决方法在编写Python时,当使用中文输出或注释时运行脚本,会提示错误信息:SyntaxError: Non-ASCII character '\xe5' in file ******* 解决方法:python的默认编码文件是用的ASCII码,你将文件存成了UTF-8!!!(文件中存在中文或者其他语言,就会出现此问题!)解决办法很简单!!!在文件开头加入: # -*- coding: UTF