Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!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.008 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'operator': 0.03; 'python': 0.09; 'empty,': 0.09; 'indexes': 0.09; 'slices': 0.09; 'subject:()': 0.09; 'index': 0.13; "can't.": 0.16; 'concatenate': 0.16; 'iterator,': 0.16; 'oct': 0.16; 'subject:array': 0.16; 'wrote:': 0.17; 'integer': 0.17; 'variables': 0.17; 'equivalent': 0.20; 'logical': 0.22; 'example': 0.23; 'linux': 0.24; 'header:In- Reply-To:1': 0.25; 'skip:[ 10': 0.26; 'replace': 0.27; 'message- id:@mail.gmail.com': 0.27; 'correct': 0.28; 'statements': 0.29; 'wrap': 0.29; 'skip:_ 10': 0.29; 'received:209.85.215.46': 0.30; 'error': 0.30; 'code': 0.31; 'print': 0.32; 'getting': 0.33; 'problem': 0.33; 'to:addr:python-list': 0.33; 'received:google.com': 0.34; 'sequence': 0.35; 'expected': 0.35; 'pm,': 0.35; 'received:209.85': 0.35; 'there': 0.35; 'but': 0.36; 'skip:{ 10': 0.36; 'does': 0.37; 'two': 0.37; 'why': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'positive': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'range': 0.60; 'remove': 0.61; 'around,': 0.84; 'lengthy': 0.84; 'to:name:python': 0.84; 'prone': 0.91; 'this!': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=YnOeHDiwAVtbk788JA1cBxYu8npASBr3GQpEerHLA7k=; b=YhcUdaMfTnmWG7mX4cwVpXBqEXDbij+ZwnMGWNhN5RhNnBlu4Zt0WejKQfO7dQExsd b335r/XD41bInaEjYZP1y9IXNwdp/q7Y412zX3ngWpquAasmKLVCKQIXzrqonfmMvKWm b8Y8dx3ZVcDlmrYmeq4GzHgolrYDpGJ42lhMZx5AN9y3SGBm15L5Tv2VHg273ocyrpf4 CLIaYv/6Qa0YOFXPsnMmtHVbXAW5jV0u7DBWfBMe97r+mPsbH9zK06Tj2LlotYg70y34 9o9xojLBcAczwdF4EJuhLOWEJp5cJh1+TtQ9Ns5shEhZxWsblRCCPgfGAeQocgiK5GKQ FSyg== MIME-Version: 1.0 In-Reply-To: <6998a955-7b34-4f4f-b8d6-62d1028f7561@googlegroups.com> References: <6998a955-7b34-4f4f-b8d6-62d1028f7561@googlegroups.com> From: Ian Kelly Date: Sun, 28 Oct 2012 21:42:50 -0600 Subject: Re: Negative array indicies and slice() To: Python Content-Type: text/plain; charset=ISO-8859-1 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: 22 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1351482209 news.xs4all.nl 6884 [2001:888:2000:d::a6]:37959 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:32331 On Sun, Oct 28, 2012 at 9:12 PM, wrote: > The slice operator does not give any way (I can find!) to take slices from negative to positive indexes, although the range is not empty, nor the expected indexes out of range that I am supplying. > > Many programs that I write would require introducing variables and logical statements to correct the problem which is very lengthy and error prone unless there is a simple work around. > > I *hate* replicating code every time I need to do this! > > I also don't understand why slice() is not equivalent to an iterator, but can replace an integer in __getitem__() whereas xrange() can't. > > > Here's an example for Linux shell, otherwise remove /bin/env... > {{{#!/bin/env python > a=[1,2,3,4,5,6,7,8,9,10] > print a[-4:3] # I am interested in getting [7,8,9,10,1,2] but I get []. > }}} For a sequence of length 10, "a[-4:3]" is equivalent to "a[6:3]", which is an empty slice since index 6 is after index 3. If you want it to wrap around, then take two slices and concatenate them with "a[-4:] + a[:3]".