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


Groups > comp.lang.python > #11924

Re: extended slicing and negative stop value problem

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.033
X-Spam-Evidence '*H*': 0.93; '*S*': 0.00; 'value,': 0.04; 'python': 0.08; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'helps!': 0.16; 'omitting': 0.16; 'subject:stop': 0.16; 'wrote:': 0.16; '>>>': 0.18; 'subject:problem': 0.19; 'header:In-Reply- To:1': 0.22; 'pm,': 0.24; 'variable': 0.24; 'aug': 0.24; 'sat,': 0.28; 'elements': 0.29; 'message-id:@mail.gmail.com': 0.29; 'definition': 0.30; '-1,': 0.30; 'semantics': 0.30; "skip:' 10": 0.30; 'to:addr:python-list': 0.33; 'idea': 0.34; 'explicit': 0.34; 'starting': 0.36; 'none': 0.37; 'using': 0.37; 'but': 0.37; 'something': 0.37; 'think': 0.38; 'received:google.com': 0.38; 'received:209.85': 0.38; 'subject:: ': 0.39; 'to:addr:python.org': 0.39; "it's": 0.40; 'your': 0.61; 'stop': 0.61; 'hope': 0.61; 'dangerous': 0.64; 'fall': 0.64; 'subject:value': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:content-transfer-encoding; bh=HknUzA67obOTDFhD60G3Qa3A4BIueRd8Bh5oSqrf7LQ=; b=I0//b7cZ0qBoOUKy8oFVckrnGjV5RSxwNMo+odJR7Zw0JNJgMS0LB4ycvvJn13mG4x far3LoOqjNNRVuSztA70D1omSrWoW3dKT8CD+PARM8QNU2+aZKwP2miLNpFc4KjWkPMZ svE4o6Guax0Ee73Gg6+ssyZ/Lhne6bRJ4J9Ec=
MIME-Version 1.0
In-Reply-To <CAOVPiMgP2kOfr83GK5uOo_-FJOPsrHJW-O_fGgnwkKfBSEQTbw@mail.gmail.com>
References <CAOVPiMgP2kOfr83GK5uOo_-FJOPsrHJW-O_fGgnwkKfBSEQTbw@mail.gmail.com>
Date Sat, 20 Aug 2011 19:29:42 +0100
Subject Re: extended slicing and negative stop value problem
From Chris Angelico <rosuav@gmail.com>
To python-list@python.org
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding quoted-printable
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.270.1313864985.27778.python-list@python.org> (permalink)
Lines 27
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1313864985 news.xs4all.nl 23934 [2001:888:2000:d::a6]:39173
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:11924

Show key headers only | View raw


On Sat, Aug 20, 2011 at 7:20 PM, Max Moroz <maxmoroz@gmail.com> wrote:
> Would it be a good idea to change Python definition so that a[10, -1, -1]
> referred to the elements starting with position 10, going down to the
> beginning?

Well, first off I think it's a dangerous idea to change semantics of
something like that. I can see your use case, but I think that what
you want is covered by simply omitting the stop marker:

>>> a="qwertyuiopasdfghjklzxcvbnm"
>>> a[10:1:-1]
'apoiuytre'
>>> a[10:0:-1]
'apoiuytrew'
>>> a[10::-1]
'apoiuytrewq'

If you're using a variable for the stop value, you just need to set it
to an explicit None if it would fall negative:

>>> a[10:None:-1]
'apoiuytrewq'

Hope that helps!

ChrisA

Back to comp.lang.python | Previous | NextNext 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