Comparing lists containing NaNs

前端 未结 1 869
余生分开走
余生分开走 2021-01-13 10:05

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 ==

相关标签:
1条回答
  • 2021-01-13 10:41

    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
    
    0 讨论(0)
提交回复
热议问题