Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@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 User-Agent: Thunderbird 1.5.0.10 (Windows/20070221) MIME-Version: 1.0 To: python-list@python.org Subject: Re: a.index(float('nan')) fails References: <5089f662$0$29984$c3e8da3$5496439d@news.astraweb.com> In-Reply-To: <5089f662$0$29984$c3e8da3$5496439d@news.astraweb.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit 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-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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: abuse@xs4all.nl Xref: csiph.com comp.lang.python:32311 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