Avatar ·

如何在循环中从字典中删除元素

📁 python, 错误, 示例

我不知怎么突然遇到了这样一个问题。在 for 循环中,我需要从字典中删除记录,但如何避免出现错误:

RuntimeError: dictionary changed size during iteration

我甚至尝试在迭代前创建字典的快照,然后从原始字典中删除。

>>> ot = {12: 'wqe', 13: 'wqe', 14: 'wqe', 15: ''}>>> ot1 = ot>>> for i in ot1:... del ot