<legend id='9FltA'><style id='9FltA'><dir id='9FltA'><q id='9FltA'></q></dir></style></legend>

  • <i id='9FltA'><tr id='9FltA'><dt id='9FltA'><q id='9FltA'><span id='9FltA'><b id='9FltA'><form id='9FltA'><ins id='9FltA'></ins><ul id='9FltA'></ul><sub id='9FltA'></sub></form><legend id='9FltA'></legend><bdo id='9FltA'><pre id='9FltA'><center id='9FltA'></center></pre></bdo></b><th id='9FltA'></th></span></q></dt></tr></i><div id='9FltA'><tfoot id='9FltA'></tfoot><dl id='9FltA'><fieldset id='9FltA'></fieldset></dl></div>

      <bdo id='9FltA'></bdo><ul id='9FltA'></ul>
  • <small id='9FltA'></small><noframes id='9FltA'>

  • <tfoot id='9FltA'></tfoot>

        如何在不调用 RuntimeError 的情况下使用循环删除 Counter 对象中的条目?

        How do I remove entries within a Counter object with a loop without invoking a RuntimeError?(如何在不调用 RuntimeError 的情况下使用循环删除 Counter 对象中的条目?)

          • <bdo id='ApW8H'></bdo><ul id='ApW8H'></ul>
            <legend id='ApW8H'><style id='ApW8H'><dir id='ApW8H'><q id='ApW8H'></q></dir></style></legend>

            <small id='ApW8H'></small><noframes id='ApW8H'>

              <tbody id='ApW8H'></tbody>
              • <tfoot id='ApW8H'></tfoot>

                  <i id='ApW8H'><tr id='ApW8H'><dt id='ApW8H'><q id='ApW8H'><span id='ApW8H'><b id='ApW8H'><form id='ApW8H'><ins id='ApW8H'></ins><ul id='ApW8H'></ul><sub id='ApW8H'></sub></form><legend id='ApW8H'></legend><bdo id='ApW8H'><pre id='ApW8H'><center id='ApW8H'></center></pre></bdo></b><th id='ApW8H'></th></span></q></dt></tr></i><div id='ApW8H'><tfoot id='ApW8H'></tfoot><dl id='ApW8H'><fieldset id='ApW8H'></fieldset></dl></div>
                1. 本文介绍了如何在不调用 RuntimeError 的情况下使用循环删除 Counter 对象中的条目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  from collections import *
                  ignore = ['the','a','if','in','it','of','or']
                  ArtofWarCounter = Counter(ArtofWarLIST)
                  for word in ArtofWarCounter:
                      if word in ignore:
                          del ArtofWarCounter[word]
                  

                  ArtofWarCounter 是一个 Counter 对象,其中包含《孙子兵法》中的所有单词.我正在尝试从 ArtofWarCounter 中删除 ignore 中的单词.

                  ArtofWarCounter is a Counter object containing all the words from the Art of War. I'm trying to have words in ignore deleted from the ArtofWarCounter.

                  追溯:

                    File "<pyshell#10>", line 1, in <module>
                      for word in ArtofWarCounter:
                  RuntimeError: dictionary changed size during iteration
                  

                  推荐答案

                  为了最少的代码更改,使用 list,这样你正在迭代的对象与 Counter 解耦代码>

                  For minimal code changes, use list, so that the object you are iterating over is decoupled from the Counter

                  ignore = ['the','a','if','in','it','of','or']
                  ArtofWarCounter = Counter(ArtofWarLIST)
                  for word in list(ArtofWarCounter):
                      if word in ignore:
                          del ArtofWarCounter[word]
                  

                  在 Python2 中,您可以使用 ArtofWarCounter.keys() 代替 list(ArtofWarCounter),但是当编写面向未来的代码如此简单时,为什么不做吗?

                  In Python2, you can use ArtofWarCounter.keys() instead of list(ArtofWarCounter), but when it is so simple to write code that is futureproofed, why not do it?

                  最好不要计算您希望忽略的项目

                  It is a better idea to just not count the items you wish to ignore

                  ignore = {'the','a','if','in','it','of','or'}
                  ArtofWarCounter = Counter(x for x in ArtofWarLIST if x not in ignore)
                  

                  请注意,我将 ignore 设置为 set,这使得测试 x not in ignore 更加高效

                  note that I made ignore into a set which makes the test x not in ignore much more efficient

                  这篇关于如何在不调用 RuntimeError 的情况下使用循环删除 Counter 对象中的条目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  What happens when you compare 2 pandas Series(当你比较 2 个 pandas 系列时会发生什么)
                  Quickly find differences between two large text files(快速查找两个大文本文件之间的差异)
                  Python - Compare 2 files and output differences(Python - 比较 2 个文件和输出差异)
                  Why do comparisions between very large float values fail in python?(为什么在 python 中非常大的浮点值之间的比较会失败?)
                  Dictionary merge by updating but not overwriting if value exists(字典通过更新合并,但如果值存在则不覆盖)
                  Find entries of one text file in another file in python(在python中的另一个文件中查找一个文本文件的条目)
                    <tfoot id='NM8LP'></tfoot>

                      <tbody id='NM8LP'></tbody>

                    <small id='NM8LP'></small><noframes id='NM8LP'>

                      <i id='NM8LP'><tr id='NM8LP'><dt id='NM8LP'><q id='NM8LP'><span id='NM8LP'><b id='NM8LP'><form id='NM8LP'><ins id='NM8LP'></ins><ul id='NM8LP'></ul><sub id='NM8LP'></sub></form><legend id='NM8LP'></legend><bdo id='NM8LP'><pre id='NM8LP'><center id='NM8LP'></center></pre></bdo></b><th id='NM8LP'></th></span></q></dt></tr></i><div id='NM8LP'><tfoot id='NM8LP'></tfoot><dl id='NM8LP'><fieldset id='NM8LP'></fieldset></dl></div>
                          <legend id='NM8LP'><style id='NM8LP'><dir id='NM8LP'><q id='NM8LP'></q></dir></style></legend>

                            <bdo id='NM8LP'></bdo><ul id='NM8LP'></ul>