是否可以把一个数设为NaN或∞?

时间:2022-09-29 00:07:33

Is it possible to set an element of an array to NaN in Python?

是否可以在Python中将数组的元素设置为NaN ?

Additionally, is it possible to set a variable to +/- infinity? If so, is there any function to check whether a number is infinity or not?

另外,是否可以将变量设置为+/-∞?如果是,是否有函数来检查一个数是否为无穷大?

4 个解决方案

#1


172  

Cast from string using float():

使用float()从字符串转换:

>>> float('NaN')
nan
>>> float('Inf')
inf
>>> -float('Inf')
-inf
>>> float('Inf') == float('Inf')
True
>>> float('Inf') == 1
False

#2


55  

Yes, you can use numpy for that.

是的,你可以用numpy。

import numpy as np
a = arange(3,dtype=float)

a[0] = np.nan
a[1] = np.inf
a[2] = -np.inf

a # is now [nan,inf,-inf]

np.isnan(a[0]) # True
np.isinf(a[1]) # True
np.isinf(a[2]) # True

#3


6  

Is it possible to set an element of an array to NaN in Python?

是否可以在Python中将数组的元素设置为NaN ?

Yes, you can always include a NaN or inf in a python list (and similar). However if you want to include it in an array (for example array.array or numpy.array) then the type of the array must be float or complex!

是的,您总是可以在python列表中包含NaN或inf(以及类似的)。但是,如果您想将它包含在数组中(例如数组)。数组或numpi .array)那么数组的类型必须是浮动的或复杂的!

>>> import math
>>> import numpy as np

>>> arr = np.ones(1, float)  # float array
>>> arr[0] = math.nan
>>> arr
array([ nan])

>>> [math.nan, math.inf, -math.inf]  # python list
[nan, inf, -inf]

The math-module contains constants for nan and inf (since Python 3.5) as well as some functions (available since Python 2.6) to check for these:

math模块包含nan和inf的常量(从Python 3.5开始)以及一些函数(从Python 2.6开始可用)来检查这些:

math.isnan checks if the value is nan:

数学。isnan检查值是否为nan:

>>> math.isnan(math.nan)   # nan
True

math.isinf checks if the value is infinity (positive or negative):

数学。检验值是否为无穷大(正数或负数):

>>> math.isinf(math.inf)   # positive infinity
True
>>> math.isinf(-math.inf)  # negative infinity
True

And a function that can check if a number is not inf or nan: math.isfinite (available for Python 3.2+)

还有一个函数,它可以检查一个数字是否为inf或nan: math。islimited(适用于Python 3.2+)

>>> math.isfinite(math.inf)
False
>>> math.isfinite(math.nan)
False
>>> math.isfinite(11.2)
True

#4


1  

When using Python 2.4, try

在使用Python 2.4时,请尝试。

inf = float("9e999")
nan = inf - inf

I am facing the issue when I was porting the simplejson to an embedded device which running the Python 2.4, float("9e999") fixed it. Don't use inf = 9e999, you need convert it from string. -inf gives the -Infinity.

当我将simplejson移植到运行Python 2.4、float(“9e999”)修复的嵌入式设备时,我遇到了这个问题。不要使用inf = 9e999,您需要从字符串转换它。负无穷。

#1


172  

Cast from string using float():

使用float()从字符串转换:

>>> float('NaN')
nan
>>> float('Inf')
inf
>>> -float('Inf')
-inf
>>> float('Inf') == float('Inf')
True
>>> float('Inf') == 1
False

#2


55  

Yes, you can use numpy for that.

是的,你可以用numpy。

import numpy as np
a = arange(3,dtype=float)

a[0] = np.nan
a[1] = np.inf
a[2] = -np.inf

a # is now [nan,inf,-inf]

np.isnan(a[0]) # True
np.isinf(a[1]) # True
np.isinf(a[2]) # True

#3


6  

Is it possible to set an element of an array to NaN in Python?

是否可以在Python中将数组的元素设置为NaN ?

Yes, you can always include a NaN or inf in a python list (and similar). However if you want to include it in an array (for example array.array or numpy.array) then the type of the array must be float or complex!

是的,您总是可以在python列表中包含NaN或inf(以及类似的)。但是,如果您想将它包含在数组中(例如数组)。数组或numpi .array)那么数组的类型必须是浮动的或复杂的!

>>> import math
>>> import numpy as np

>>> arr = np.ones(1, float)  # float array
>>> arr[0] = math.nan
>>> arr
array([ nan])

>>> [math.nan, math.inf, -math.inf]  # python list
[nan, inf, -inf]

The math-module contains constants for nan and inf (since Python 3.5) as well as some functions (available since Python 2.6) to check for these:

math模块包含nan和inf的常量(从Python 3.5开始)以及一些函数(从Python 2.6开始可用)来检查这些:

math.isnan checks if the value is nan:

数学。isnan检查值是否为nan:

>>> math.isnan(math.nan)   # nan
True

math.isinf checks if the value is infinity (positive or negative):

数学。检验值是否为无穷大(正数或负数):

>>> math.isinf(math.inf)   # positive infinity
True
>>> math.isinf(-math.inf)  # negative infinity
True

And a function that can check if a number is not inf or nan: math.isfinite (available for Python 3.2+)

还有一个函数,它可以检查一个数字是否为inf或nan: math。islimited(适用于Python 3.2+)

>>> math.isfinite(math.inf)
False
>>> math.isfinite(math.nan)
False
>>> math.isfinite(11.2)
True

#4


1  

When using Python 2.4, try

在使用Python 2.4时,请尝试。

inf = float("9e999")
nan = inf - inf

I am facing the issue when I was porting the simplejson to an embedded device which running the Python 2.4, float("9e999") fixed it. Don't use inf = 9e999, you need convert it from string. -inf gives the -Infinity.

当我将simplejson移植到运行Python 2.4、float(“9e999”)修复的嵌入式设备时,我遇到了这个问题。不要使用inf = 9e999,您需要从字符串转换它。负无穷。