How to Check list containing NaN

前端 未结 3 1529
余生分开走
余生分开走 2021-02-19 19:51

In my for loop, my code generates a list like this one:

list([0.0,0.0]/sum([0.0,0.0]))

The loop generates all sort of other number vectors but

相关标签:
3条回答
  • 2021-02-19 19:52

    May be this is what you are looking for...

    a = [2,3,np.nan]
    b = True if True in np.isnan(np.array(a)) else False
    print(b)
    
    0 讨论(0)
  • You should use the math module.

    >>> import math
    >>> math.isnan(item)
    
    0 讨论(0)
  • 2021-02-19 20:11

    I think this makes sense because of your pulling numpy into scope indirectly via the star import.

    >>> import numpy as np
    >>> [0.0,0.0]/0
    Traceback (most recent call last):
      File "<ipython-input-3-aae9e30b3430>", line 1, in <module>
        [0.0,0.0]/0
    TypeError: unsupported operand type(s) for /: 'list' and 'int'
    
    >>> [0.0,0.0]/np.float64(0)
    array([ nan,  nan])
    

    When you did

    from matplotlib.pylab import *
    

    it pulled in numpy.sum:

    >>> from matplotlib.pylab import *
    >>> sum is np.sum
    True
    >>> [0.0,0.0]/sum([0.0, 0.0])
    array([ nan,  nan])
    

    You can test that this nan object (nan isn't unique in general) is in a list via identity, but if you try it in an array it seems to test via equality, and nan != nan:

    >>> nan == nan
    False
    >>> nan == nan, nan is nan
    (False, True)
    >>> nan in [nan]
    True
    >>> nan in np.array([nan])
    False
    

    You could use np.isnan:

    >>> np.isnan([nan, nan])
    array([ True,  True], dtype=bool)
    >>> np.isnan([nan, nan]).any()
    True
    
    0 讨论(0)
提交回复
热议问题