• <legend id='2T7mA'><style id='2T7mA'><dir id='2T7mA'><q id='2T7mA'></q></dir></style></legend>

    <small id='2T7mA'></small><noframes id='2T7mA'>

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

        在运算符中,float(“NaN") 和 np.nan

        in operator, float(quot;NaNquot;) and np.nan(在运算符中,float(“NaN) 和 np.nan)

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

              <tbody id='2yN3s'></tbody>
              <bdo id='2yN3s'></bdo><ul id='2yN3s'></ul>

            • <small id='2yN3s'></small><noframes id='2yN3s'>

                • <legend id='2yN3s'><style id='2yN3s'><dir id='2yN3s'><q id='2yN3s'></q></dir></style></legend>
                  本文介绍了在运算符中,float(“NaN") 和 np.nan的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我曾经相信 Python 中的 in 运算符使用相等性检查 == 来检查某个集合中元素的存在,所以 element in some_list 大致相当于 any(x == element for x in some_list).例如:

                  I used to believe that in operator in Python checks the presence of element in some collection using equality checking ==, so element in some_list is roughly equivalent to any(x == element for x in some_list). For example:

                  True in [1, 2, 3]
                  # True because True == 1
                  

                  1 in [1., 2., 3.]
                  # also True because 1 == 1.
                  

                  然而,众所周知 NaN 不等于自身.所以我预计 [float("NaN")] 中的 float("NaN") 是 False.确实是False.

                  However, it is well-known that NaN is not equal to itself. So I expected that float("NaN") in [float("NaN")] is False. And it is False indeed.

                  但是,如果我们使用 numpy.nan 而不是 float("NaN"),情况就大不相同了:

                  However, if we use numpy.nan instead of float("NaN"), the situation is quite different:

                  import numpy as np
                  np.nan in [np.nan, 1, 2]
                  # True
                  

                  但是 np.nan == np.nan 仍然给出 False!

                  这怎么可能?np.nanfloat("NaN") 有什么区别?in 如何处理np.nan?

                  How is it possible? What's the difference between np.nan and float("NaN")? How does in deal with np.nan?

                  推荐答案

                  为了检查项目是否在列表中,Python 测试对象身份首先,然后仅测试对象是否相等是不同的.1

                  To check if the item is in the list, Python tests for object identity first, and then tests for equality only if the objects are different.1

                  float("NaN") 为 False,因为在比较.因此,身份测试返回 False,然后相等性测试也返回 False,因为 NaN != NaN.

                  np.nan in [np.nan, 1, 2] 然而是 True 因为 same NaN 对象参与比较.对象身份的测试返回 True,因此 Python 立即将项目识别为在列表中.

                  np.nan in [np.nan, 1, 2] however is True because the same NaN object is involved in the comparison. The test for object identity returns True and so Python immediately recognises the item as being in the list.

                  Python 的许多其他内置容器类型(例如元组和集合)的 __contains__ 方法(使用 in 调用)使用相同的检查来实现.

                  The __contains__ method (invoked using in) for many of Python's other builtin Container types, such as tuples and sets, is implemented using the same check.

                  1 至少在 CPython 中是这样.这里的对象标识意味着在相同的内存地址找到对象:包含列表方法 使用 PyObject_RichCompareBool 在可能更复杂的对象比较之前快速比较对象指针.其他 Python 实现可能会有所不同.

                  1 At least this is true in CPython. Object identity here means that the objects are found at the same memory address: the contains method for lists is performed using PyObject_RichCompareBool which quickly compares object pointers before a potentially more complicated object comparison. Other Python implementations may differ.

                  这篇关于在运算符中,float(“NaN") 和 np.nan的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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中的另一个文件中查找一个文本文件的条目)
                    <bdo id='wT9es'></bdo><ul id='wT9es'></ul>

                        • <legend id='wT9es'><style id='wT9es'><dir id='wT9es'><q id='wT9es'></q></dir></style></legend>

                            <tbody id='wT9es'></tbody>
                          <tfoot id='wT9es'></tfoot>

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

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