Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion a.index(float('nan')) fails

Received: by 10.66.75.39 with SMTP id z7mr10743371pav.26.1351544638760;
        Mon, 29 Oct 2012 14:03:58 -0700 (PDT)
Path: 6ni50602pbd.1!nntp.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newspeer1.nac.net!news.mi.ras.ru!spln!extra.newsguy.com!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path: <et...@stoneleaf.us>
X-Original-To: python-l...@python.org
Delivered-To: python-l...@mail.python.org
X-Spam-Status: OK 0.000
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'anyway.': 0.04;
	'function,': 0.07; 'python': 0.09; '"""return': 0.09;
	'from:addr:ethan': 0.09; 'from:addr:stoneleaf.us': 0.09;
	'from:name:ethan furman': 0.09; 'message-id:@stoneleaf.us': 0.09;
	'received:184.172': 0.09; 'received:gator410.hostgator.com': 0.09;
	'themselves,': 0.09; 'def': 0.10; 'index': 0.13; 'equality.':
	0.16; 'isnan': 0.16; 'nan': 0.16; 'nans': 0.16; 'subject:fails':
	0.16; 'unequal': 0.16; 'wrote:': 0.17; 'tests': 0.18; 'math':
	0.20; 'versions': 0.20; 'import': 0.21; 'this:': 0.23; 'header:In-
	Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'first,': 0.27;
	"d'aprano": 0.29; 'steven': 0.29; 'probably': 0.29; 'problem':
	0.33; 'to:addr:python-list': 0.33; 'that,': 0.34; 'identity':
	0.35; 'list.': 0.35; 'compare': 0.36; 'method': 0.36; 'uses':
	0.37; 'item': 0.37; 'subject:: ': 0.38; 'to:addr:python.org':
	0.39; 'header:Received:5': 0.40; 'your': 0.60; 'first': 0.61;
	'provide': 0.62; 'different': 0.63; 'received:69.56': 0.65
Date: Sun, 28 Oct 2012 06:07:52 -0700
From: Ethan Furman <et...@stoneleaf.us>
User-Agent: Thunderbird 1.5.0.10 (Windows/20070221)
MIME-Version: 1.0
To: python-l...@python.org
Subject: Re: a.index(float('nan')) fails
References: <bd80bfd0-b423-418f-a338-fea626d50093@googlegroups.com>
	<5089f662$0$29984$c3e8da3$5496439d@news.astraweb.com>
In-Reply-To: <5089f662$0$29984$c3e8da3$5496439d@news.astraweb.com>
X-AntiAbuse: This header was added to track abuse,
	please include it with any abuse report
X-AntiAbuse: Primary Hostname - gator410.hostgator.com
X-AntiAbuse: Original Domain - python.org
X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12]
X-AntiAbuse: Sender Address Domain - stoneleaf.us
X-BWhitelist: no
X-Source: 
X-Source-Args: 
X-Source-Dir: 
X-Source-Sender: ([192.168.11.4]) [173.12.184.238]:49250
X-Source-Auth: ethan+stoneleaf.us
X-Email-Count: 1
X-Source-Cap: dG9idWs7dG9idWs7Z2F0b3I0MTAuaG9zdGdhdG9yLmNvbQ==
X-BeenThere: python-l...@python.org
X-Mailman-Version: 2.1.15
Precedence: list
List-Id: General discussion list for the Python programming language
	<python-list.python.org>
List-Unsubscribe: <http://mail.python.org/mailman/options/python-list>,
	<mailto:python-list-requ...@python.org?subject=unsubscribe>
List-Archive: <http://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-l...@python.org>
List-Help: <mailto:python-list-requ...@python.org?subject=help>
List-Subscribe: <http://mail.python.org/mailman/listinfo/python-list>,
	<mailto:python-list-requ...@python.org?subject=subscribe>
Newsgroups: comp.lang.python
Message-ID: <mailman.2969.1351430163.27098.python-l...@python.org>
Lines: 27
NNTP-Posting-Host: 2001:888:2000:d::a6
X-Trace: 1351430163 news.xs4all.nl 6957 [2001:888:2000:d::a6]:38396
X-Complaints-To: ab...@xs4all.nl
Bytes: 4139
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

Steven D'Aprano wrote:
> The list.index method tests for the item with equality. Since NANs are 
> mandated to compare unequal to anything, including themselves, index 
> cannot match them.

This is incorrect.  .index() uses identity first, then equality, and 
will match the same NaN in a list.  The OP's problem was in using a 
different NaN.

Having said that, your find_nan() solution is probably the one to use 
anyway.

> from math import isnan
> 
> def find_nan(seq):
>     """Return the index of the first NAN in seq, otherwise None."""
>     for i, x in enumerate(seq):
>         if isnan(x):
>             return i
> 
> 
> For old versions of Python that don't provide an isnan function, you can 
> do this:
> 
> def isnan(x):
>     return x != x