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

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

    1. <legend id='FSNLU'><style id='FSNLU'><dir id='FSNLU'><q id='FSNLU'></q></dir></style></legend>
      1. <tfoot id='FSNLU'></tfoot>
      2. <i id='FSNLU'><tr id='FSNLU'><dt id='FSNLU'><q id='FSNLU'><span id='FSNLU'><b id='FSNLU'><form id='FSNLU'><ins id='FSNLU'></ins><ul id='FSNLU'></ul><sub id='FSNLU'></sub></form><legend id='FSNLU'></legend><bdo id='FSNLU'><pre id='FSNLU'><center id='FSNLU'></center></pre></bdo></b><th id='FSNLU'></th></span></q></dt></tr></i><div id='FSNLU'><tfoot id='FSNLU'></tfoot><dl id='FSNLU'><fieldset id='FSNLU'></fieldset></dl></div>
      3. python vs numpy中的布尔和类型检查

        时间:2023-09-01

          <legend id='pnH7X'><style id='pnH7X'><dir id='pnH7X'><q id='pnH7X'></q></dir></style></legend>

            <bdo id='pnH7X'></bdo><ul id='pnH7X'></ul>
            <tfoot id='pnH7X'></tfoot>
          • <small id='pnH7X'></small><noframes id='pnH7X'>

                  <i id='pnH7X'><tr id='pnH7X'><dt id='pnH7X'><q id='pnH7X'><span id='pnH7X'><b id='pnH7X'><form id='pnH7X'><ins id='pnH7X'></ins><ul id='pnH7X'></ul><sub id='pnH7X'></sub></form><legend id='pnH7X'></legend><bdo id='pnH7X'><pre id='pnH7X'><center id='pnH7X'></center></pre></bdo></b><th id='pnH7X'></th></span></q></dt></tr></i><div id='pnH7X'><tfoot id='pnH7X'></tfoot><dl id='pnH7X'><fieldset id='pnH7X'></fieldset></dl></div>
                    <tbody id='pnH7X'></tbody>
                  本文介绍了python vs numpy中的布尔和类型检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我今天在 python if 子句中遇到了意想不到的结果:

                  I ran into unexpected results in a python if clause today:

                  import numpy
                  if numpy.allclose(6.0, 6.1, rtol=0, atol=0.5):
                      print 'close enough'  # works as expected (prints message)
                  
                  if numpy.allclose(6.0, 6.1, rtol=0, atol=0.5) is True:
                      print 'close enough'  # does NOT work as expected (prints nothing)
                  

                  经过一番探索(即这个问题,特别是this answer),我明白了原因:numpy.allclose()返回的typenumpy.bool_ 而不是普通的旧 bool,显然如果 foo = numpy.bool_(1),那么 if foo 将评估为 Trueif foo 为 True 将评估为 False.这似乎是 is 运算符的工作.

                  After some poking around (i.e., this question, and in particular this answer), I understand the cause: the type returned by numpy.allclose() is numpy.bool_ rather than plain old bool, and apparently if foo = numpy.bool_(1), then if foo will evaluate to True while if foo is True will evaluate to False. This appears to be the work of the is operator.

                  我的问题是:为什么 numpy 有自己的布尔类型,鉴于这种情况,最佳实践是什么?在上面的示例中,我可以通过编写 if foo: 来获得预期的行为,但我喜欢更严格的 if foo is True: 因为它排除了像 2[2] 从返回 True,有时需要显式类型检查.

                  My questions are: why does numpy have its own boolean type, and what is best practice in light of this situation? I can get away with writing if foo: to get expected behavior in the example above, but I like the more stringent if foo is True: because it excludes things like 2 and [2] from returning True, and sometimes the explicit type check is desirable.

                  推荐答案

                  为什么 numpy 有自己的布尔类型

                  why does numpy have its own boolean type

                  空间和速度.Numpy 将事物存储在紧凑的数组中;如果它可以将布尔值放入单个字节中,它会尝试.对于 Python 对象,您无法轻松做到这一点,因为您必须存储引用,这会显着降低计算速度.

                  Space and speed. Numpy stores things in compact arrays; if it can fit a boolean into a single byte it'll try. You can't easily do this with Python objects, as you have to store references which slows calculations down significantly.

                  我可以写 if foo: 来获得上面示例中的预期行为,但我喜欢更严格的 if foo is True: 因为它排除了像 2 和 [2] 之类的东西返回 True,有时是显式的类型检查是可取的.

                  I can get away with writing if foo: to get expected behavior in the example above, but I like the more stringent if foo is True: because it excludes things like 2 and [2] from returning True, and sometimes the explicit type check is desirable.

                  好吧,不要那样做.

                  这篇关于python vs numpy中的布尔和类型检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:检查 False 的正确方法是什么? 下一篇:错误“只能比较标记相同的系列对象"和 sort_index

                  相关文章

                  <legend id='PufSZ'><style id='PufSZ'><dir id='PufSZ'><q id='PufSZ'></q></dir></style></legend>
                • <tfoot id='PufSZ'></tfoot>

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

                    • <bdo id='PufSZ'></bdo><ul id='PufSZ'></ul>

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