Python RuntimeError: dictionary changed size during iteration 错误解决方法
"Traceback (most recent call last):\n File "C:\Users\Lenovo\Desktop\pycharm\hebei - \u590d\u5236\run\test.py", line 16, in \u003cmodule\u003e\n for i, j in v.items():\nRuntimeError: dictionary changed size during iteration"\n\n\u8fd9\u4e2a\u9519\u8bef\u662f\u7531\u4e8e\u5728\u8f6e\u626b\u5b57\u5178\u65f6\uff0c\u5b57\u5178\u7684\u5927\u5c0f\u53d1\u751f\u4e86\u6539\u53d8\u5bfc\u81f4\u7684\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5728\u5faa\u73af\u8f6e\u626b\u5b57\u5178\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4f60\u5bf9\u5b57\u5178\u8fdb\u884c\u4e86\u589e\u52a0\u6216\u5220\u9664\u5143\u7d20\u7684\u64cd\u4f5c\uff0c\u8fd9\u6837\u4f1a\u5bfc\u81f4\u8f6e\u626b\u5668\u65e0\u6cd5\u6b63\u786e\u7684\u8f6e\u626b\u5b57\u5178\u3002\n\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u4e00\u4e2a\u4e34\u65f6\u7684\u52a9\u7528\u590d\u5236\u6765\u8fdb\u884c\u8f6e\u626b\uff0c\u800c\u4e0d\u662f\u76f4\u63a5\u5728\u8f6e\u626b\u8fc7\u7a0b\u4e2d\u4fee\u6539\u539f\u5b57\u5178\u3002\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u5982\u4e0b\uff1a\n\npython\n\n v = {'a': 1, 'b': 2, 'c': 3}\n\n # \u521b\u5efa\u4e00\u4e2a\u5b57\u5178\u7684\u590d\u5236\uff0c\u5e76\u5bf9\u590d\u5236\u8fdb\u884c\u64cd\u4f5c\n temp_v = v.copy\(\)\n for i, j in temp_v.items\(\):\n if j == 2:\n del v[i]\n\n print\(v\)\n\n\n\n\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u5728\u8f6e\u626b\u8fc7\u7a0b\u4e2d\u4fee\u6539\u5b57\u5178\u800c\u5f15\u8d77\u7684\u9519\u8bef\u3002
原文地址: https://www.cveoy.top/t/topic/qsg8 著作权归作者所有。请勿转载和采集!