Comparing lists containing NaNs

I am trying to compare two different lists to see if they are equal, and was going to remove NaNs, only to discover that my list comparisons still work, despite NaN == NaN -> False .

Could someone explain why the following evaluate True or False , as I am finding this behavior unexpected. Thanks,

I have read the following which don't seem to resolve the issue:

  • Why in numpy nan == nan is False while nan in [nan] is True?
  • Why is NaN not equal to NaN? [duplicate]
  • (Python 2.7.3, numpy-1.9.2)

    I have marked surprising evaluations with a * at the end

    >>> nan = np.nan
    >>> [1,2,3]==[3]
    False
    >>> [1,2,3]==[1,2,3]
    True
    >>> [1,2,nan]==[1,2,nan]
    True ***
    >>> nan == nan
    False
    >>> [nan] == [nan]
    True ***
    >>> [nan, nan] == [nan for i in range(2)]
    True ***
    >>> [nan, nan] == [float(nan) for i in range(2)]
    True ***
    >>> float(nan) is (float(nan) + 1)
    False
    >>> float(nan) is float(nan)
    True ***
    

    To understand what happens here, simply replace nan = np.nan by foo = float('nan') , you will get exactly the same result, why?

    >>> foo = float('nan')
    >>> foo is foo # This is obviously True! 
    True
    >>> foo == foo # This is False per the standard (nan != nan).
    False
    >>> bar = float('nan') # foo and bar are two different objects.
    >>> foo is bar
    False
    >>> foo is float(foo) # "Tricky", but float(x) is x if type(x) == float.
    True
    

    Now think that numpy.nan is just a variable name that holds a float('nan') .

    Now why [nan] == [nan] is simply because list comparison first test identity equality between items before equality for value, think of it as:

    def equals(l1, l2):
        for u, v in zip(l1, l2):
            if u is not v and u != v:
                return False
        return True
    
    链接地址: http://www.djcxy.com/p/73354.html

    上一篇: javascript null条件表达式

    下一篇: 比较包含NaN的列表