本文介绍了字典在迭代过程中更改大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我浏览了与该主题相关的答案的数据库,找不到答案,本质上是我在浏览字典,得到字典更改大小,运行时错误,但我却突然弹出取出一个键和值,并在迭代恢复之前插入另一个键和值。

I looked through the database of answers pertaining to this topic and couldn't find an answer, essentially I'm looping through a dictionary, I'm getting the, "dictionary changes size," runtime error yet I'm popping out one key and value and inserting another before the iteration resumes.

       for patterns in dict_copy.keys():
            new_tuple = ()
            for items in range(len(patterns)):
                if patters[items] not in exclusion:
                    new_tuple += (patterns[items],)
            dict_copy[new_tuple] = dict_copy.get(patterns)
            dict_copy.pop(patterns)

我正在使用的词典的格式为:{ ( A, B, C, D):4,( B, A, C, D) 2 ...}
我是它认为我正在改变字典的大小这一事实几乎使我感到困惑

The dictionaries I'm working with are in the form: {("A","B","C","D"):4, ("B","A","C","D") "2...}I'm pretty much just confused over the fact that it thinks I'm chaning the dictionary size

推荐答案

这没关系。您不能
Python的迭代器变得混乱(-:这与字典的大小无关,而是它的内容。
(在o中是相同的方式)还有其他编程语言...)

That does not matter. You cannot change a data structure while iterating it.Python's iterator gets confused (-: It's not about the size of the dictionary, but its content.(It's the same way in other programming languages too...)

这篇关于字典在迭代过程中更改大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 07:38
查看更多