Path: csiph.com!usenet.pasdenom.info!news.albasani.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!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; 'else:': 0.03; 'classes.': 0.07; 'differently': 0.07; 'indices': 0.07; 'python': 0.09; '"if': 0.09; 'negative,': 0.09; 'slices': 0.09; 'span': 0.09; 'subject:()': 0.09; 'def': 0.10; 'programmer': 0.11; 'language': 0.14; 'do,': 0.15; '10:00': 0.16; 'integers.': 0.16; 'oct': 0.16; 'ranges.': 0.16; 'sequence.': 0.16; 'stop):': 0.16; 'stop.': 0.16; 'subject:array': 0.16; 'xrange': 0.16; 'wrote:': 0.17; 'header:In- Reply-To:1': 0.25; 'skip:[ 10': 0.26; 'possibly': 0.27; 'realize': 0.27; 'separate': 0.27; 'question': 0.27; 'start,': 0.27; 'message-id:@mail.gmail.com': 0.27; 'decide': 0.28; 'equivalent.': 0.29; 'it."': 0.29; 'wrap': 0.29; 'writes:': 0.29; 'array': 0.29; 'convert': 0.29; 'operate': 0.32; 'anyone': 0.33; 'to:addr:python- list': 0.33; "can't": 0.34; 'received:google.com': 0.34; 'list': 0.35; 'sequence': 0.35; 'pm,': 0.35; 'received:209.85': 0.35; 'really': 0.36; 'but': 0.36; 'totally': 0.36; 'does': 0.37; 'two': 0.37; 'why': 0.37; 'passed': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'positive': 0.38; 'object': 0.38; 'instead': 0.39; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'end': 0.40; 'your': 0.60; "you'll": 0.62; 'charset:windows-1252': 0.65; 'choose': 0.65; 'stated': 0.69; 'ranges': 0.71; 'yourself': 0.77; 'ian,': 0.84; 'start.': 0.84; 'stop,': 0.84; 'to:name:python': 0.84 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=bAy+jn80Y8Otw8KJf640ftsBxmb2sRiCrVsxOViMbpY=; b=iZvQysRoBPLX0baV8r8tb2m3TqlDFnG5jKKJf2jLzs3N9HFoH7uNNce8NdXcPlWvWv fG7Y6OMbqd555o9EdL7wapMvRV4CjNgOUxujRpIrvdq2W27koRePoGfw1h8fass8Jq/7 AulOtzqHOVUzuIYxxbu9gieeCzHoxgQ1U/Rm7Jmc9Fa2hFjCCHpLqwJEq+I/g0C66SJO tELmwTSuTt4rmIWVd7B426QmcVhYYQPOOQiKzrn4G5nLeWPbZo2iar5nEvvvuCqtkqey kbL032Edm9xwSiBaX8llasEsFXPwDnLzkGZM92SwF1XsgL6tOFmxCZ/stuIA4SmbXq1W hiXw== MIME-Version: 1.0 In-Reply-To: <4c024364-84df-403b-8b9e-4a4c8f06121c@googlegroups.com> References: <6998a955-7b34-4f4f-b8d6-62d1028f7561@googlegroups.com> <4c024364-84df-403b-8b9e-4a4c8f06121c@googlegroups.com> From: Ian Kelly Date: Sun, 28 Oct 2012 22:25:28 -0600 Subject: Re: Negative array indicies and slice() To: Python Content-Type: text/plain; charset=windows-1252 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: 36 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1351484760 news.xs4all.nl 6937 [2001:888:2000:d::a6]:59873 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:32337 On Sun, Oct 28, 2012 at 10:00 PM, wrote: > Hi Ian, > Well, no it really isn't equivalent. > Consider a programmer who writes: > xrange(-4,3) *wants* [-4,-3,-2,-1,0,1,2] > > That is the "idea" of a range; for what reason would anyone *EVER* want -= 4 to +3 to be 6:3??? That is what ranges do, but your question was about slices, not ranges. > So: Why does python choose to convert them to positive indexes, and have = slice operate differently than xrange -- for the slice() object can't possi= bly know the size of the array when it is passed in to __getitem__; They a= re totally separate classes. Ranges can contain negative integers. However, sequences do not have negative indices. Therefore, negative indices in slices are used to count from the end instead of from the start. As stated in the language docs, "If either bound is negative, the sequence=92s length is added to it." Therefore, "a[-4:3]" does not wrap around the end of the sequence because "a[6:3]" does not wrap around the end of the sequence. > I realize I can concat. two slice ranges, BUT, the ranges do not always s= pan from negative to positive. def wrapping_slice(seq, start, stop): start, stop, _ =3D slice(start, stop).indices(len(seq)) if start <=3D stop: return seq[start:stop] else: return seq[start:] + seq[:stop] You'll have to decide for yourself whether you want it to return an empty list or the entire list if start =3D=3D stop.