Discuss / Python / 有个小问题请教

有个小问题请教

Topic source

Freddiemars

#1 Created at ... [Delete] [Delete and Lock User]

d={'a':88,'b':66,'c':56} n=1 for key,value in d.items(): d['n']=n n=n+1 if n>10: break print(d)

运行的时候显示: RuntimeError: dictionary changed size during iteration 希望好心人解救

应该在for循环之前,给字典添加新的键值对 d = {'a': 88, 'b': 66, 'c': 56} n = 1 d['n'] = n for k, v in d.items(): n = n + 1 d['n'] = n if n > 10: break print(d)


  • 1

Reply