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