我正在尝试比较两个不同的列表以查看它们是否相等,并且要删除NaN,只是发现我的列表比较仍然有效,尽管NaN == NaN -> False
。
有人可以解释为什么以下评估True
或False
,因为我发现此行为是意外的。谢谢,
我已阅读以下内容似乎无法解决此问题:
nan == nan
is False while nan in [nan] is True? (Python 2.7.3,numpy-1.9.2)
我在最后用*
标记了令人惊讶的评价
>>> 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 ***
答案 0 :(得分:1)
要了解此处发生的情况,只需将nan = np.nan
替换为foo = float('nan')
,您将获得完全相同的结果,为什么?
>>> 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
现在认为numpy.nan
只是一个包含float('nan')
的变量名。
现在为什么[nan] == [nan]
只是因为list
比较首先在值之间的项之间测试身份相等,将其视为:
def equals(l1, l2):
for u, v in zip(l1, l2):
if u is not v and u != v:
return False
return True