Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeder.news-service.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!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; 'cpython': 0.05; 'interpreter': 0.07; 'pypy': 0.07; 'python': 0.08; '21,': 0.09; 'presume': 0.09; 'received:209.85.160.174': 0.09; 'received:mail- gy0-f174.google.com': 0.09; 'runtime': 0.09; 'this).': 0.09; 'am,': 0.12; "doesn't.": 0.16; 'reversing': 0.16; 'slicing': 0.16; 'subject:stop': 0.16; 'cc:addr:python-list': 0.16; 'wrote:': 0.16; 'cheers,': 0.18; 'subject:problem': 0.19; '(which': 0.19; 'cc:no real name:2**0': 0.20; "doesn't": 0.22; 'cc:2**0': 0.22; 'assume': 0.22; 'header:In-Reply-To:1': 0.22; 'work,': 0.23; 'slice': 0.23; 'though.': 0.23; 'pm,': 0.24; 'aug': 0.24; 'code': 0.25; 'sat,': 0.28; 'elements': 0.29; 'looks': 0.29; 'message- id:@mail.gmail.com': 0.29; 'second': 0.29; 'cc:addr:python.org': 0.30; 'definition': 0.30; '-1,': 0.30; 'semantics': 0.30; 'sun,': 0.30; 'usual': 0.31; 'chris': 0.32; 'time:': 0.32; 'instead': 0.33; 'idea': 0.34; 'surprised': 0.34; 'received:209.85.160': 0.35; 'certain': 0.35; 'starting': 0.36; 'but': 0.37; 'could': 0.38; 'steven': 0.38; 'received:google.com': 0.38; 'received:209.85': 0.38; 'should': 0.38; 'subject:: ': 0.39; 'case': 0.39; 'your': 0.61; 'sender:addr:chris': 0.84; 'subject:value': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rebertia.com; s=google; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=xH6AhfgEkhXyaCHs36vmC10171a9e1+L6lkq5dCfxp4=; b=H0JpN1Ejmp6uu+fMRItENNL5sp9auV7Okv62+L4Pu98mFs7qP4i3y93XxNTYWMNzX8 Af7lJTZ3KtssqNNf+rA1CCMaB42oqFE2UB2vPIWWXqM6v0Wa3LsPBIdW+fVdoTm5nt9e EHihSvIHORyA3mRTLCH5ulS8s2a+0+aj16f3g= MIME-Version: 1.0 Sender: chris@rebertia.com In-Reply-To: <7488461a-de1f-4e7c-ba7f-a15d85896e08@y39g2000prd.googlegroups.com> References: <4e501ba1$0$29970$c3e8da3$5496439d@news.astraweb.com> <7488461a-de1f-4e7c-ba7f-a15d85896e08@y39g2000prd.googlegroups.com> Date: Sun, 21 Aug 2011 15:16:21 -0700 X-Google-Sender-Auth: CCz5hRwRE_KiubUs9QRExbHtA8g Subject: Re: extended slicing and negative stop value problem From: Chris Rebert To: Max Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 30 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1313964991 news.xs4all.nl 23937 [2001:888:2000:d::a6]:48990 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:11966 On Sun, Aug 21, 2011 at 10:27 AM, Max wrote: > On Aug 20, 1:40=C2=A0pm, Steven D'Aprano +comp.lang.pyt...@pearwood.info> wrote: >> On Sat, Aug 20, 2011 at 7:20 PM, Max Moroz wrote: >> > Would it be a good idea to change Python definition so that a[10, -1, = -1] >> >> I presume you mean slice notation a[10:-1:-1]. >> >> > referred to the elements starting with position 10, going down to the >> > beginning? >> The usual advice is to do your slicing twice, reversing it the second ti= me: >> >> a[0:11][::-1] >> # Instead of a[10:-1:-1], which looks like it should work, but doesn't. > > It works nicely, but it is 1.3 times slower in my code (I am surprised > the interpreter doesn't optimize this). That would require CPython to assume certain slicing semantics for all types (which it can't) or to check for this very specific case at runtime (which would slow down all other [list] slicing operations). A smarter implementation such as PyPy could indeed theoretically optimize this case though. Cheers, Chris