要了解这里发生的情况,只需替换
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 barFalse>>> 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



