Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'initialize': 0.05; 'defines': 0.07; 'indices': 0.07; 'python': 0.09; '*end*': 0.09; 'array.': 0.09; 'indexes': 0.09; 'pointers': 0.09; 'subject:()': 0.09; "'c',": 0.16; 'arithmetic.': 0.16; 'hardware.': 0.16; 'iterator': 0.16; 'merely': 0.16; 'oct': 0.16; 'routinely': 0.16; 'sequence.': 0.16; 'subject:array': 0.16; 'thought.': 0.16; 'mon,': 0.16; 'wrote:': 0.17; 'issue,': 0.17; 'pointer': 0.17; 'written,': 0.17; 'variable': 0.20; 'question.': 0.20; 'elements': 0.23; 'this:': 0.23; 'header:In-Reply-To:1': 0.25; 'leave': 0.26; 'extend': 0.26; 'am,': 0.27; 'question': 0.27; 'andrew': 0.27; 'message-id:@mail.gmail.com': 0.27; 'chris': 0.28; 'arrays': 0.29; 'wrap': 0.29; 'array': 0.29; 'case,': 0.29; 'skip:_ 10': 0.29; "i'm": 0.29; 'classes': 0.30; 'that.': 0.30; 'received:209.85.215.46': 0.30; 'lists': 0.31; 'could': 0.32; 'to:addr:python-list': 0.33; 'received:google.com': 0.34; 'list': 0.35; 'returning': 0.35; 'sequence': 0.35; 'received:209.85': 0.35; 'wanted': 0.36; 'data.': 0.36; "i'll": 0.36; 'test': 0.36; 'possible': 0.37; 'received:209': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'positive': 0.38; 'skip:l 20': 0.38; 'sure': 0.38; 'instead': 0.39; 'to:addr:python.org': 0.39; 'where': 0.40; 'header:Received:5': 0.40; 'end': 0.40; 'your': 0.60; 'map': 0.61; 'solve': 0.62; 'behavior': 0.64; 'want,': 0.65; 'fact,': 0.69; 'notice:': 0.71; 'contrary': 0.71; 'obvious': 0.71; 'to:name:python': 0.84; 'you;': 0.84; 'faster.': 0.91 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:content-transfer-encoding; bh=ZutKMBbP04gmu7dzVpIdTIt/64ZPFw31c6X1SQqYVt8=; b=uZDpaRfGA2MLF/WZkTdWSWC2UO62e1PucHCREkfnDiLysIUxHwqAYXCeYG90UncJHm Xp+AEkHcU9FqHGz2Jz9wSlPT57O5sgnGFDgqEMGjGAnJpPFMB8ANoXViHpRvDS2P94hB LX70S2rWp2kc9k7J2DCAKzsnKGc58DgYEPNG8zzea6kjZBvyLfoEjZOI6kOlLcGTotMB As7N3bekJCzEEv04mrjZLtZmKtozuXSwOgaA8VuCgo+8BmoSLP6mdGrLHJ+IM87FcFyr 7XVI00FVx1tssCG41+SI6jIwDRoCFuMTNP4UiS5mYCeTUIgsdu1ZPOYOz5xeoWSfgp7G VZOw== MIME-Version: 1.0 In-Reply-To: References: <6998a955-7b34-4f4f-b8d6-62d1028f7561@googlegroups.com> <4c024364-84df-403b-8b9e-4a4c8f06121c@googlegroups.com> From: Ian Kelly Date: Mon, 29 Oct 2012 11:27:47 -0600 Subject: Re: Negative array indicies and slice() To: Python Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable 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: 30 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1351531699 news.xs4all.nl 6913 [2001:888:2000:d::a6]:47633 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:32408 On Mon, Oct 29, 2012 at 1:54 AM, Andrew wrote: > My intended inferences about the iterator vs. slice question was perhaps = not obvious to you; Notice: an iterator is not *allowed* in __getitem__(). Yes, I misconstrued your question. I thought you wanted to change the behavior of slicing to wrap around the end when start > stop instead of returning an empty sequence. What you actually want is a new sequence built from indexes supplied by an iterable. Chris has already given you a list comprehension solution to solve that. You could also use map for this: new_seq =3D list(map(old_seq.__getitem__, iterable)) Since you seem to be concerned about performance, I'm not sure in this case whether the map or the list comprehension will be faster. I'll leave you to test that on your intended hardware. > In 'C', where Python is written, circularly linked lists -- and arrays ar= e both very efficient ways of accessing data. Arrays can, in fact, have ne= gative indexes -- perhaps contrary to what you thought. One merely defines= a variable to act as the base pointer to the array and initialize it to th= e *end* of the array. Nor is the size of the data elements an issue, since = in Python all classes are accessed by pointers which are of uniform size. I= routinely do this in C. I'm aware of what is possible in C with pointer arithmetic. This is Python, though, and Python by design has neither pointers nor pointer arithmetic. In any case, initializing the pointer to the end of the array would still not do what you want, since the positive indices would then extend past the end of the array.