python SortedDict 遍历删除 不对

Posted 软件工程小施同学

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python SortedDict 遍历删除 不对相关的知识,希望对你有一定的参考价值。

topLevel = SortedDict(neg) # 从大到小排序


            for priority, Id in topLevel.items():
                print("topLevel1", topLevel)
         
                # # 将Id从topLevel中删除
                topLevel.pop(priority)
                
                print("topLevel2", self.Txpool.topLevel)

发现4000011: 11没有遍历就跳出了

修改后

            tmp = [(k,v) for k,v in topLevel.items()]
            print(len(tmp))
            for priority, Id in tmp:
                print("topLevel1", topLevel)
         
                # # 将Id从topLevel中删除
                topLevel.pop(priority)
                
                print("topLevel2", self.Txpool.topLevel)

以上是关于python SortedDict 遍历删除 不对的主要内容,如果未能解决你的问题,请参考以下文章

Python 优先级字典SortedDict 用法实例

Python为啥不能用列表遍历来删除列表中的相同的元素呢?

python3中,定义一个函数,遍历字符串,使用print后调用函数会有None,换成return结果又不对?

java集合遍历删除指定元素异常分析总结

C#遍历List并删除某个元素的方法

Python遍历列表时删除元素