<tfoot id='OCNAh'></tfoot>
      • <bdo id='OCNAh'></bdo><ul id='OCNAh'></ul>

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

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

      1. Python中set.discard和set.remove方法之间的运行时差异?

        Runtime difference between set.discard and set.remove methods in Python?(Python中set.discard和set.remove方法之间的运行时差异?)

              • <small id='6xOKw'></small><noframes id='6xOKw'>

              • <i id='6xOKw'><tr id='6xOKw'><dt id='6xOKw'><q id='6xOKw'><span id='6xOKw'><b id='6xOKw'><form id='6xOKw'><ins id='6xOKw'></ins><ul id='6xOKw'></ul><sub id='6xOKw'></sub></form><legend id='6xOKw'></legend><bdo id='6xOKw'><pre id='6xOKw'><center id='6xOKw'></center></pre></bdo></b><th id='6xOKw'></th></span></q></dt></tr></i><div id='6xOKw'><tfoot id='6xOKw'></tfoot><dl id='6xOKw'><fieldset id='6xOKw'></fieldset></dl></div>
                <legend id='6xOKw'><style id='6xOKw'><dir id='6xOKw'><q id='6xOKw'></q></dir></style></legend>
                  <bdo id='6xOKw'></bdo><ul id='6xOKw'></ul>
                  <tfoot id='6xOKw'></tfoot>
                    <tbody id='6xOKw'></tbody>
                • 本文介绍了Python中set.discard和set.remove方法之间的运行时差异?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  这些方法的官方 Python 2.7 文档听起来几乎相同,与唯一的区别似乎是 remove() 会引发 KeyError 而 discard 不会.

                  我想知道这两种方法的执行速度是否存在差异.如果做不到这一点,它们之间是否有任何有意义的区别(除了 KeyError)?

                  解决方案

                  在一种情况下引发异常是一个非常有意义的区别.如果尝试从不存在的集合中删除元素会出错,则最好使用 set.remove() 而不是 set.discard().p>

                  这两种方法在实现上是相同的,除了相比 set_discard() set_remove()函数添加行:

                  if (rv == DISCARD_NOTFOUND) {set_key_error(key);返回空值;}

                  这会引发 KeyError.由于这需要更多的工作,set.remove()teeniest 慢一点;您的 CPU 在返回之前必须进行一项额外的测试.但是,如果您的算法依赖于异常,那么额外的分支测试就无关紧要了.

                  The official Python 2.7 docs for these methods sounds nearly identical, with the sole difference seeming to be that remove() raises a KeyError while discard does not.

                  I'm wondering if there is a difference in execution speed between these two methods. Failing that, is there any meaningful difference (barring KeyError) between them?

                  解决方案

                  Raising an exception in one case is a pretty meaningful difference. If trying to remove an element from a set that is not there would be an error, you better use set.remove() rather than set.discard().

                  The two methods are identical in implementation, except that compared to set_discard() the set_remove() function adds the lines:

                  if (rv == DISCARD_NOTFOUND) {
                      set_key_error(key);
                      return NULL;
                  }
                  

                  This raises the KeyError. As this is slightly more work, set.remove() is a teeniest fraction slower; your CPU has to do one extra test before returning. But if your algorithm depends on the exception then the extra branching test is hardly going to matter.

                  这篇关于Python中set.discard和set.remove方法之间的运行时差异?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  env: python: No such file or directory(env: python: 没有这样的文件或目录)
                  How to evaluate environment variables into a string in Python?(如何在 Python 中将环境变量评估为字符串?)
                  Python - temporarily modify the current process#39;s environment(Python - 临时修改当前进程的环境)
                  Change current process environment#39;s LD_LIBRARY_PATH(更改当前进程环境的 LD_LIBRARY_PATH)
                  Reading and writing environment variables in Python?(在 Python 中读写环境变量?)
                  When to use sys.path.append and when modifying %PYTHONPATH% is enough(何时使用 sys.path.append 以及何时修改 %PYTHONPATH% 就足够了)

                          <tbody id='EumRz'></tbody>
                      • <legend id='EumRz'><style id='EumRz'><dir id='EumRz'><q id='EumRz'></q></dir></style></legend>

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

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

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