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


Groups > comp.lang.python > #11966

Re: extended slicing and negative stop value problem

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 <chris@rebertia.com>
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 <CAOVPiMgP2kOfr83GK5uOo_-FJOPsrHJW-O_fGgnwkKfBSEQTbw@mail.gmail.com> <mailman.270.1313864985.27778.python-list@python.org> <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 <clp2@rebertia.com>
To Max <maxmoroz@gmail.com>
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 <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.289.1313964991.27778.python-list@python.org> (permalink)
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

Show key headers only | View raw


On Sun, Aug 21, 2011 at 10:27 AM, Max <maxmoroz@gmail.com> wrote:
> On Aug 20, 1:40 pm, Steven D'Aprano <steve
> +comp.lang.pyt...@pearwood.info> wrote:
>> On Sat, Aug 20, 2011 at 7:20 PM, Max Moroz <maxmo...@gmail.com> 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?
<snip>
>> The usual advice is to do your slicing twice, reversing it the second time:
>>
>> 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

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


Thread

Re: extended slicing and negative stop value problem Chris Angelico <rosuav@gmail.com> - 2011-08-20 19:29 +0100
  Re: extended slicing and negative stop value problem Max <maxmoroz@gmail.com> - 2011-08-20 11:52 -0700
    Re: extended slicing and negative stop value problem Chris Angelico <rosuav@gmail.com> - 2011-08-20 20:53 +0100
  Re: extended slicing and negative stop value problem Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-08-21 06:40 +1000
    Re: extended slicing and negative stop value problem Max <maxmoroz@gmail.com> - 2011-08-21 10:27 -0700
      Re: extended slicing and negative stop value problem Chris Rebert <clp2@rebertia.com> - 2011-08-21 15:16 -0700

csiph-web