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

      在没有numpy的python中分配一个变量NaN

      Assigning a variable NaN in python without numpy(在没有numpy的python中分配一个变量NaN)
      <i id='SSITy'><tr id='SSITy'><dt id='SSITy'><q id='SSITy'><span id='SSITy'><b id='SSITy'><form id='SSITy'><ins id='SSITy'></ins><ul id='SSITy'></ul><sub id='SSITy'></sub></form><legend id='SSITy'></legend><bdo id='SSITy'><pre id='SSITy'><center id='SSITy'></center></pre></bdo></b><th id='SSITy'></th></span></q></dt></tr></i><div id='SSITy'><tfoot id='SSITy'></tfoot><dl id='SSITy'><fieldset id='SSITy'></fieldset></dl></div>

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

            <legend id='SSITy'><style id='SSITy'><dir id='SSITy'><q id='SSITy'></q></dir></style></legend>
                <bdo id='SSITy'></bdo><ul id='SSITy'></ul>
                <tfoot id='SSITy'></tfoot>
                  <tbody id='SSITy'></tbody>
                本文介绍了在没有numpy的python中分配一个变量NaN的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                大多数语言都有一个 NaN 常量,您可以使用它来为变量分配值 NaN.python可以不使用numpy做到这一点吗?

                Most languages have a NaN constant you can use to assign a variable the value NaN. Can python do this without using numpy?

                推荐答案

                是的 -- 使用 math.nan.

                Yes -- use math.nan.

                >>> from math import nan
                >>> print(nan)
                nan
                >>> print(nan + 2)
                nan
                >>> nan == nan
                False
                >>> import math
                >>> math.isnan(nan)
                True
                

                在 Python 3.5 之前,可以使用 float("nan")(不区分大小写).

                Before Python 3.5, one could use float("nan") (case insensitive).

                请注意,检查是否为 NaN 的两个事物是否彼此相等将始终返回 false.这部分是因为不是数字"的两件事不能(严格来说)说彼此相等 - 请参阅 对于 IEEE754 NaN 值返回 false 的所有比较的基本原理是什么?了解更多详细信息和信息.

                Note that checking to see if two things that are NaN are equal to one another will always return false. This is in part because two things that are "not a number" cannot (strictly speaking) be said to be equal to one another -- see What is the rationale for all comparisons returning false for IEEE754 NaN values? for more details and information.

                改为使用 math.isnan(...) 是否需要判断一个值是否为 NaN.

                Instead, use math.isnan(...) if you need to determine if a value is NaN or not.

                此外,在尝试将 NaN 存储在 listdict 等容器类型中时,对 NaN 值的 == 操作的确切语义可能会导致微妙的问题(或使用自定义容器类型时).有关详细信息,请参阅检查容器中是否存在 NaN.

                Furthermore, the exact semantics of the == operation on NaN value may cause subtle issues when trying to store NaN inside container types such as list or dict (or when using custom container types). See Checking for NaN presence in a container for more details.

                您还可以使用 Python 的 decimal 模块构造 NaN 数:

                You can also construct NaN numbers using Python's decimal module:

                >>> from decimal import Decimal
                >>> b = Decimal('nan')
                >>> print(b)
                NaN
                >>> print(repr(b))
                Decimal('NaN')
                >>>
                >>> Decimal(float('nan'))
                Decimal('NaN')
                >>> 
                >>> import math
                >>> math.isnan(b)
                True
                

                math.isnan(...) 也适用于 Decimal 对象.

                math.isnan(...) will also work with Decimal objects.

                但是,您不能在 Python 的 分数 模块:

                However, you cannot construct NaN numbers in Python's fractions module:

                >>> from fractions import Fraction
                >>> Fraction('nan')
                Traceback (most recent call last):
                  File "<stdin>", line 1, in <module>
                  File "C:Python35libfractions.py", line 146, in __new__
                    numerator)
                ValueError: Invalid literal for Fraction: 'nan'
                >>>
                >>> Fraction(float('nan'))
                Traceback (most recent call last):
                  File "<stdin>", line 1, in <module>
                  File "C:Python35libfractions.py", line 130, in __new__
                    value = Fraction.from_float(numerator)
                  File "C:Python35libfractions.py", line 214, in from_float
                    raise ValueError("Cannot convert %r to %s." % (f, cls.__name__))
                ValueError: Cannot convert nan to Fraction.
                

                <小时>

                顺便说一句,您也可以使用 float('Inf')Decimal('Inf')math.inf (3.5+) 分配无限数.(另请参阅 math.isinf(...))

                但是,Fraction('Inf')Fraction(float('inf')) 是不允许的,并且会抛出异常,就像 NaN 一样.

                However doing Fraction('Inf') or Fraction(float('inf')) isn't permitted and will throw an exception, just like NaN.

                如果您想要一种快速简便的方法来检查一个数字是否既不是 NaN 也不是无限的,您可以使用 math.isfinite(...) 自 Python 3.2+ 起.

                If you want a quick and easy way to check if a number is neither NaN nor infinite, you can use math.isfinite(...) as of Python 3.2+.

                如果您想对复数进行类似的检查,cmath 模块包含一组与 math 模块类似的函数和常量:

                If you want to do similar checks with complex numbers, the cmath module contains a similar set of functions and constants as the math module:

                • cmath.isnan(...)
                • cmath.isinf(...)
                • cmath.isfinite(...) (Python 3.2+)
                • cmath.nan(Python 3.6+;相当于 complex(float('nan'), 0.0))
                • cmath.nanj(Python 3.6+;等价于 complex(0.0, float('nan')))
                • cmath.inf(Python 3.6+;相当于 complex(float('inf'), 0.0))
                • cmath.infj(Python 3.6+;相当于 complex(0.0, float('inf')))

                这篇关于在没有numpy的python中分配一个变量NaN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                相关文档推荐

                Running .jl file from R or Python(从 R 或 Python 运行 .jl 文件)
                Running Julia .jl file in python(在 python 中运行 Julia .jl 文件)
                Using PIP in a Azure WebApp(在 Azure WebApp 中使用 PIP)
                How to run python3.7 based flask web api on azure(如何在 azure 上运行基于 python3.7 的烧瓶 web api)
                Azure Python Web App Internal Server Error(Azure Python Web 应用程序内部服务器错误)
                Run python dlib library on azure app service(在 azure app 服务上运行 python dlib 库)
              • <i id='15w5P'><tr id='15w5P'><dt id='15w5P'><q id='15w5P'><span id='15w5P'><b id='15w5P'><form id='15w5P'><ins id='15w5P'></ins><ul id='15w5P'></ul><sub id='15w5P'></sub></form><legend id='15w5P'></legend><bdo id='15w5P'><pre id='15w5P'><center id='15w5P'></center></pre></bdo></b><th id='15w5P'></th></span></q></dt></tr></i><div id='15w5P'><tfoot id='15w5P'></tfoot><dl id='15w5P'><fieldset id='15w5P'></fieldset></dl></div>

                    <small id='15w5P'></small><noframes id='15w5P'>

                    <legend id='15w5P'><style id='15w5P'><dir id='15w5P'><q id='15w5P'></q></dir></style></legend>

                  1. <tfoot id='15w5P'></tfoot>

                      <bdo id='15w5P'></bdo><ul id='15w5P'></ul>
                        <tbody id='15w5P'></tbody>