Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #32199

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

From Terry Reedy <tjreedy@udel.edu>
Subject Re: a.index(float('nan')) fails
Date 2012-10-26 04:00 -0400
References <bd80bfd0-b423-418f-a338-fea626d50093@googlegroups.com> <k6cr4c$4md$1@ger.gmane.org> <5089F33A.8010804@mrabarnett.plus.com>
Newsgroups comp.lang.python
Message-ID <mailman.2886.1351238424.27098.python-list@python.org> (permalink)

Show all headers | View raw


On 10/25/2012 10:19 PM, MRAB wrote:
> On 2012-10-26 03:04, Terry Reedy wrote:
>> On 10/25/2012 9:46 PM, mamboknave@gmail.com wrote:
>>>>>> a = [float('nan'), 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>>>>> a
>>> [nan, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>>>>> a.index(float('nan'))

This is a second nan object, and it is not in the list.

>>> Traceback (most recent call last):
>>>    File "<stdin>", line 1, in <module>
>>> ValueError: list.index(x): x not in list
>>>
>>> That means, the function .index() cannot detect nan values.
>>> It happens on both Python 2.6 and Python 3.1
>>>
>>> Is this a bug? Or I am not using .index() correctly?
>>
>> It is a consequence of the following, which some people (but not all)
>> believe is mandated by the IEEE standard.
>>
>>   >>> nan = float('nan')
>>   >>> nan is nan
>> True
>>   >>> nan == nan
>> False
>>
>>   >>> nanlist = [nan]
>>   >>> nan in nanlist
>> True
>>   >>> nanlist.index(nan)
>> 0

.index found the nan.

>> Containment of nan in collection is tested by is, not ==.
>>
>>   >>> nan2 = float('nan')
>>   >>> nan2 is nan
>> False
>>   >>> nan2 == nan
>> False
>>   >>> nan2 in nanlist
>> False
>>
> 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. This inconsistency is an intentional decision to 
not propagate the insanity of nan != nan to Python collections.

-- 
Terry Jan Reedy

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

a.index(float('nan')) fails mamboknave@gmail.com - 2012-10-25 18:46 -0700
  Re: a.index(float('nan')) fails Terry Reedy <tjreedy@udel.edu> - 2012-10-25 22:04 -0400
    Re: a.index(float('nan')) fails Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-10-26 02:44 +0000
      Re: a.index(float('nan')) fails Terry Reedy <tjreedy@udel.edu> - 2012-10-26 03:54 -0400
        Re: a.index(float('nan')) fails Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-10-26 15:26 +0000
          Re: a.index(float('nan')) fails Terry Reedy <tjreedy@udel.edu> - 2012-10-26 14:49 -0400
    Re: a.index(float('nan')) fails Nobody <nobody@nowhere.com> - 2012-10-27 17:40 +0100
  Re: a.index(float('nan')) fails Cameron Simpson <cs@zip.com.au> - 2012-10-26 13:15 +1100
    Re: a.index(float('nan')) fails mamboknave@gmail.com - 2012-10-25 19:22 -0700
    Re: a.index(float('nan')) fails mamboknave@gmail.com - 2012-10-25 19:22 -0700
  Re: a.index(float('nan')) fails MRAB <python@mrabarnett.plus.com> - 2012-10-26 03:19 +0100
  Re: a.index(float('nan')) fails Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-10-26 02:33 +0000
    Re: a.index(float('nan')) fails Ethan Furman <ethan@stoneleaf.us> - 2012-10-28 06:07 -0700
  Re: a.index(float('nan')) fails Terry Reedy <tjreedy@udel.edu> - 2012-10-26 04:00 -0400
    Re: a.index(float('nan')) fails Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-10-26 16:23 +0000
      Re: a.index(float('nan')) fails MRAB <python@mrabarnett.plus.com> - 2012-10-26 17:43 +0100
      Re: a.index(float('nan')) fails Chris Angelico <rosuav@gmail.com> - 2012-10-27 03:45 +1100
        Re: a.index(float('nan')) fails Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-10-26 18:40 +0000
          Re: a.index(float('nan')) fails Devin Jeanpierre <jeanpierreda@gmail.com> - 2012-10-26 15:17 -0400
          Re: a.index(float('nan')) fails Chris Angelico <rosuav@gmail.com> - 2012-10-27 12:29 +1100
      Re: a.index(float('nan')) fails Terry Reedy <tjreedy@udel.edu> - 2012-10-26 14:58 -0400
      Re: a.index(float('nan')) fails Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-10-27 00:48 -0400
        Re: a.index(float('nan')) fails Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2012-10-27 08:56 +0200
          Re: a.index(float('nan')) fails Nobody <nobody@nowhere.com> - 2012-10-27 17:45 +0100
          Re: a.index(float('nan')) fails Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-10-27 13:07 -0400
  Re: a.index(float('nan')) fails Mark Adam <dreamingforward@gmail.com> - 2012-10-27 13:33 -0500

csiph-web