a.index(float('nan')) fails

MRAB python at mrabarnett.plus.com
Fri Oct 26 12:43:08 EDT 2012


On 2012-10-26 17:23, Steven D'Aprano wrote:
> On Fri, 26 Oct 2012 04:00:03 -0400, Terry Reedy wrote:
>
>> On 10/25/2012 10:19 PM, MRAB wrote:
>
>>> In summary, .index() looks for an item which is equal to its argument,
>>> but it's a feature of NaN (as defined by the standard) that it doesn't
>>> equal NaN, therefore .index() will never find it.
>>
>> Except that is *does* find the particular nan object that is in the
>> collection. So nan in collection and list.index(nan) look for the nan by
>> identity, not equality.
>
> So it does. I made the same mistake as MRAB, thank you for the correction.
>
[snip]
Yes, I forgot that Python checks for identity before checking for
equality:

 >>> [float("nan")].index(float("nan"))
Traceback (most recent call last):
   File "<pyshell#0>", line 1, in <module>
     [float("nan")].index(float("nan"))
ValueError: nan is not in list
 >>> nan = float("nan")
 >>> [nan].index(nan)
0




More information about the Python-list mailing list