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


Groups > comp.lang.python > #32329 > unrolled thread

Negative array indicies and slice()

Started byandrewr3mail@gmail.com
First post2012-10-28 20:12 -0700
Last post2012-11-01 18:08 -0700
Articles 13 on this page of 73 — 16 participants

Back to article view | Back to comp.lang.python


Contents

  Negative array indicies and slice() andrewr3mail@gmail.com - 2012-10-28 20:12 -0700
    Re: Negative array indicies and slice() Ian Kelly <ian.g.kelly@gmail.com> - 2012-10-28 21:42 -0600
      Re: Negative array indicies and slice() andrewr3mail@gmail.com - 2012-10-28 21:00 -0700
        Re: Negative array indicies and slice() Ian Kelly <ian.g.kelly@gmail.com> - 2012-10-28 22:25 -0600
          Re: Negative array indicies and slice() Andrew <andrewr3mail@gmail.com> - 2012-10-29 00:54 -0700
            Re: Negative array indicies and slice() Chris Rebert <clp2@rebertia.com> - 2012-10-29 01:18 -0700
            Re: Negative array indicies and slice() Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-10-29 11:19 +0000
              Re: Negative array indicies and slice() Chris Angelico <rosuav@gmail.com> - 2012-10-29 22:32 +1100
              Re: Negative array indicies and slice() Andrew Robinson <andrew3@r3dsolutions.com> - 2012-10-28 21:52 -0700
              Re: Negative array indicies and slice() Chris Angelico <rosuav@gmail.com> - 2012-10-29 23:40 +1100
                Re: Negative array indicies and slice() Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-10-29 22:02 +0000
              Re: Negative array indicies and slice() Andrew Robinson <andrew3@r3dsolutions.com> - 2012-10-28 23:01 -0700
                Re: Negative array indicies and slice() Roy Smith <roy@panix.com> - 2012-10-29 09:52 -0400
                  Re: Negative array indicies and slice() Andrew Robinson <andrew3@r3dsolutions.com> - 2012-10-29 08:20 -0700
                  Re: Negative array indicies and slice() Ian Kelly <ian.g.kelly@gmail.com> - 2012-10-29 17:01 -0600
                  Re: Negative array indicies and slice() Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2012-10-30 00:04 +0000
                  Re: Negative array indicies and slice() Andrew Robinson <andrew3@r3dsolutions.com> - 2012-10-29 16:54 -0700
                  Re: Negative array indicies and slice() Ian Kelly <ian.g.kelly@gmail.com> - 2012-10-30 02:15 -0600
              Re: Negative array indicies and slice() Chris Angelico <rosuav@gmail.com> - 2012-10-30 00:53 +1100
              Re: Negative array indicies and slice() Ian Kelly <ian.g.kelly@gmail.com> - 2012-10-29 11:09 -0600
              Re: Negative array indicies and slice() Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-10-29 22:14 +0000
              Re: Negative array indicies and slice() Andrew Robinson <andrew3@r3dsolutions.com> - 2012-10-29 08:42 -0700
                Re: Negative array indicies and slice() Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-10-30 00:02 +0000
                  Re: Negative array indicies and slice() Andrew Robinson <andrew3@r3dsolutions.com> - 2012-10-29 12:34 -0700
                    Re: Negative array indicies and slice() Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-10-30 08:17 +0000
                      Re: Negative array indicies and slice() Andrew Robinson <andrew3@r3dsolutions.com> - 2012-10-30 08:47 -0700
                      Re: Negative array indicies and slice() Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-10-30 23:48 +0000
                      Re: Negative array indicies and slice() Michael Torrie <torriem@gmail.com> - 2012-10-30 23:29 -0600
                  Re: Negative array indicies and slice() Michael Torrie <torriem@gmail.com> - 2012-10-29 23:53 -0600
                  Re: Negative array indicies and slice() Andrew Robinson <andrew3@r3dsolutions.com> - 2012-10-29 17:04 -0700
              Re: Negative array indicies and slice() Chris Angelico <rosuav@gmail.com> - 2012-10-30 09:55 +1100
              Re: Negative array indicies and slice() Ian Kelly <ian.g.kelly@gmail.com> - 2012-10-29 17:07 -0600
                Re: Negative array indicies and slice() Roy Smith <roy@panix.com> - 2012-10-29 19:24 -0400
                  Re: Negative array indicies and slice() Ian Kelly <ian.g.kelly@gmail.com> - 2012-10-29 17:43 -0600
                    Re: Negative array indicies and slice() Roy Smith <roy@panix.com> - 2012-10-29 20:17 -0400
                  Re: Negative array indicies and slice() Ian Kelly <ian.g.kelly@gmail.com> - 2012-10-29 18:05 -0600
              Re: Negative array indicies and slice() Andrew Robinson <andrew3@r3dsolutions.com> - 2012-10-29 11:00 -0700
              Re: Negative array indicies and slice() Chris Kaynor <ckaynor@zindagigames.com> - 2012-10-29 18:49 -0700
              Re: Negative array indicies and slice() Andrew Robinson <andrew3@r3dsolutions.com> - 2012-10-29 15:39 -0700
              Re: Negative array indicies and slice() Ian Kelly <ian.g.kelly@gmail.com> - 2012-10-29 23:55 -0600
              Re: Negative array indicies and slice() Ian Kelly <ian.g.kelly@gmail.com> - 2012-10-30 00:51 -0600
              Re: Negative array indicies and slice() Andrew Robinson <andrew3@r3dsolutions.com> - 2012-10-29 17:17 -0700
              Re: Negative array indicies and slice() Ian Kelly <ian.g.kelly@gmail.com> - 2012-10-30 01:21 -0600
              Re: Negative array indicies and slice() Ian Kelly <ian.g.kelly@gmail.com> - 2012-10-30 01:32 -0600
              Re: Negative array indicies and slice() Ian Kelly <ian.g.kelly@gmail.com> - 2012-10-30 02:46 -0600
              Re: Negative array indicies and slice() Ian Kelly <ian.g.kelly@gmail.com> - 2012-10-30 12:02 -0600
              Re: Negative array indicies and slice() Andrew Robinson <andrew3@r3dsolutions.com> - 2012-10-30 07:21 -0700
              Re: Negative array indicies and slice() Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-10-30 21:33 +0000
                Re: Negative array indicies and slice() Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-10-31 10:07 +0000
                  Re: Negative array indicies and slice() Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-10-31 16:01 +0000
              Re: Negative array indicies and slice() Ian Kelly <ian.g.kelly@gmail.com> - 2012-10-30 15:47 -0600
              Re: Negative array indicies and slice() Ian Kelly <ian.g.kelly@gmail.com> - 2012-10-30 15:55 -0600
              Re: Negative array indicies and slice() Chris Angelico <rosuav@gmail.com> - 2012-10-31 09:00 +1100
              Re: Negative array indicies and slice() Ian Kelly <ian.g.kelly@gmail.com> - 2012-10-30 16:02 -0600
              Re: Negative array indicies and slice() Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-10-30 23:30 +0000
            Re: Negative array indicies and slice() Ian Kelly <ian.g.kelly@gmail.com> - 2012-10-29 11:27 -0600
            Re: Negative array indicies and slice() Andrew Robinson <andrew3@r3dsolutions.com> - 2012-10-29 16:33 -0700
          Re: Negative array indicies and slice() Andrew <andrewr3mail@gmail.com> - 2012-10-29 00:54 -0700
      Re: Negative array indicies and slice() andrewr3mail@gmail.com - 2012-10-28 21:00 -0700
      Re: Negative array indicies and slice() Andrew <andrewr3mail@gmail.com> - 2012-10-28 21:09 -0700
        Re: Negative array indicies and slice() alex23 <wuwei23@gmail.com> - 2012-10-28 21:44 -0700
          Re: Negative array indicies and slice() andrewr3mail@gmail.com - 2012-10-29 01:24 -0700
            Re: Negative array indicies and slice() Chris Rebert <clp2@rebertia.com> - 2012-10-29 01:37 -0700
              Re: Negative array indicies and slice() andrewr3mail@gmail.com - 2012-10-29 01:59 -0700
                Re: Negative array indicies and slice() Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-10-29 09:36 +0000
                Re: Negative array indicies and slice() Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-10-29 10:34 +0000
              Re: Negative array indicies and slice() andrewr3mail@gmail.com - 2012-10-29 01:59 -0700
        Re: Negative array indicies and slice() Paul Rubin <no.email@nospam.invalid> - 2012-10-28 22:14 -0700
          Re: Negative array indicies and slice() andrewr3mail@gmail.com - 2012-10-29 01:08 -0700
            Re: Negative array indicies and slice() Chris Rebert <clp2@rebertia.com> - 2012-10-29 01:26 -0700
      Re: Negative array indicies and slice() Andrew <andrewr3mail@gmail.com> - 2012-10-28 21:09 -0700
    Re: Negative array indicies and slice() MRAB <python@mrabarnett.plus.com> - 2012-10-29 03:45 +0000
    Re: Negative array indicies and slice() 88888 Dihedral <dihedral88888@googlemail.com> - 2012-11-01 18:08 -0700

Page 4 of 4 — ← Prev page 1 2 3 [4]


#32339

Fromalex23 <wuwei23@gmail.com>
Date2012-10-28 21:44 -0700
Message-ID<0215c39b-47f3-46c5-9173-598f0136a881@g7g2000pbi.googlegroups.com>
In reply to#32335
On Oct 29, 2:09 pm, Andrew <andrewr3m...@gmail.com> wrote:
> I use this arbitrary range code *often* so I need a general purpose solution.
> I looked up slice() but the help is of no use, I don't even know how I might
> overload it to embed some logic to concatenate ranges of data; nor even if
> it is possible.

Slices are passed in if provided to __getitem__/__setitem__/
__delitem__, so you'd need to override it at the list level:

    class RangedSlicer(list):
        def __getitem__(self, item):
            # map item.start, .stop and .step to your own semantics

Then wrap your lists with your RangedSlicer class as needed.

[toc] | [prev] | [next] | [standalone]


#32349

Fromandrewr3mail@gmail.com
Date2012-10-29 01:24 -0700
Message-ID<b50501de-b204-4e47-b907-0a249281b3d1@googlegroups.com>
In reply to#32339
On Sunday, October 28, 2012 9:44:56 PM UTC-7, alex23 wrote:
> On Oct 29, 2:09 pm, Andrew <andrewr3m...@gmail.com> wrote:
> 
> > I use this arbitrary range code *often* so I need a general purpose solution.
> 
> > I looked up slice() but the help is of no use, I don't even know how I might
> 
> > overload it to embed some logic to concatenate ranges of data; nor even if
> 
> > it is possible.
> 
> 
> 
> Slices are passed in if provided to __getitem__/__setitem__/
> 
> __delitem__, so you'd need to override it at the list level:
> 
> 
> 
>     class RangedSlicer(list):
> 
>         def __getitem__(self, item):
> 
>             # map item.start, .stop and .step to your own semantics
> 
> 
> 
> Then wrap your lists with your RangedSlicer class as needed.

Hmmm...

I began a test in an interactive shell:
>>> class RangedSlicer(list):
...     def __getitem__(self,item):
...             print item
... 
>>> a=[1,2,3,4,5]
>>> a.__getitem__( slice(1,5) )
[2, 3, 4, 5]

Very odd...  I would have expected [1,2,3,4]

>>> a.__getitem__( slice(1,8) )
[2, 3, 4, 5]

So, slice() somehow was truncated although it ought to have been executed first, and passed to __getitem__() before __getitem__ could affect it.
That requires some tricky programming!

Not only that, but,
a.__getitem__( xrange[1,8] )
Causes an exception before the __getitem__ shadowing received it.

I don't see how I can over-ride it with your suggestion, but that's very inconsistent.... for your idea seems to be normal python that would work for user defined classes.

[toc] | [prev] | [next] | [standalone]


#32351

FromChris Rebert <clp2@rebertia.com>
Date2012-10-29 01:37 -0700
Message-ID<mailman.2999.1351499869.27098.python-list@python.org>
In reply to#32349
On Mon, Oct 29, 2012 at 1:24 AM,  <andrewr3mail@gmail.com> wrote:
> On Sunday, October 28, 2012 9:44:56 PM UTC-7, alex23 wrote:
>> On Oct 29, 2:09 pm, Andrew <andrewr3m...@gmail.com> wrote:
<snip>
>>     class RangedSlicer(list):
<snip>
>> Then wrap your lists with your RangedSlicer class as needed.
>
> Hmmm...
>
> I began a test in an interactive shell:
>>>> class RangedSlicer(list):
> ...     def __getitem__(self,item):
> ...             print item
> …

This just defines a class; it doesn't modify in-place the normal
behavior of plain lists. You have to actually *use* the class.

>>>> a=[1,2,3,4,5]

You never wrapped `a` in a RangedSlicer or otherwise made use of RangedSlicer!
You wanted:
a = RangedSlicer([1,2,3,4,5])

>>>> a.__getitem__( slice(1,5) )
> [2, 3, 4, 5]
>
> Very odd...  I would have expected [1,2,3,4]

"[2, 3, 4, 5]" is the return value from `a.__getitem__( slice(1,5) )`
(or, equivalently, from `[1,2,3,4,5][1:5]`). It is not the result of
"print item"; that line of code is never executed since you never used
the RangedSlicer class at all.

Regards,
Chris

[toc] | [prev] | [next] | [standalone]


#32352

Fromandrewr3mail@gmail.com
Date2012-10-29 01:59 -0700
Message-ID<b0ec0510-401c-46c6-b3b5-f3e829f40afa@googlegroups.com>
In reply to#32351
On Monday, October 29, 2012 1:38:04 AM UTC-7, Chris Rebert wrote:
> On Mon, Oct 29, 2012 at 1:24 AM, 
> 
> > On Sunday, October 28, 2012 9:44:56 PM UTC-7, alex23 wrote:
> 
> >> On Oct 29, 2:09 pm, Andrew < wrote:
> 
> You never wrapped `a` in a RangedSlicer or otherwise made use of RangedSlicer!
> 
> You wanted:
> 
> a = RangedSlicer([1,2,3,4,5])
> 
> 
> 
> >>>> a.__getitem__( slice(1,5) )
> 
> > [2, 3, 4, 5]
> 
> >
> 
> > Very odd...  I would have expected [1,2,3,4]
> 
> 
> 
> "[2, 3, 4, 5]" is the return value from `a.__getitem__( slice(1,5) )`
> 
> (or, equivalently, from `[1,2,3,4,5][1:5]`). It is not the result of
> 
> "print item"; that line of code is never executed since you never used
> 
> the RangedSlicer class at all.
> 
> 
> 
> Regards,
> 
> Chris

My apology --- I deleted that post; yet it didn't delete... I saw my mistake seconds after posting.

***** gmail.

Note: I subscribed to the python-list, and am able to recieve e-mails, but I don't see how to write a post for this particular thread nor subscribe to this particular thread...

A brief suggestion, or link to a howto would be *much* appreciated.

[toc] | [prev] | [next] | [standalone]


#32354

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2012-10-29 09:36 +0000
Message-ID<mailman.3001.1351503186.27098.python-list@python.org>
In reply to#32352
On 29/10/2012 08:59, andrewr3mail@gmail.com wrote:
>
> Note: I subscribed to the python-list, and am able to recieve e-mails, but I don't see how to write a post for this particular thread nor subscribe to this particular thread...
>
> A brief suggestion, or link to a howto would be *much* appreciated.
>

Get yourself a decent email client.  I read all the Python lists that 
I'm interested in using Thunderbird on Windows via gmane.

-- 
Cheers.

Mark Lawrence.

[toc] | [prev] | [next] | [standalone]


#32357

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2012-10-29 10:34 +0000
Message-ID<508e5bbd$0$29967$c3e8da3$5496439d@news.astraweb.com>
In reply to#32352
On Mon, 29 Oct 2012 01:59:06 -0700, andrewr3mail wrote:

> Note: I subscribed to the python-list, and am able to recieve e-mails,
> but I don't see how to write a post for this particular thread nor
> subscribe to this particular thread...

The beauty of email is that you don't have to subscribe to a thread. Once 
you subscribe to the mailing list, email is delivered into your inbox. To 
reply to it, just reply to it. To ignore it, throw it in the trash.

Gmail should have a button or three that say "Reply to email" or similar. 
You want the button that says "Reply to All" or "Reply to List". Make 
sure that the reply includes "python-list@python.org" as a recipient.

Delete bits of the quoted email (the lines that start with > characters) 
that are no longer relevant to the conversation. Type your reply. Double 
check that the reply is going to python-list. Then hit Send.

(P.S. when you signed up for python-list@python.org, if you selected the 
option to receive a single daily digest instead of individual emails, 
you're going to have a bad time. Do yourself a favour -- and the rest of 
us -- and change back to individual emails.)



-- 
Steven

[toc] | [prev] | [next] | [standalone]


#32353

Fromandrewr3mail@gmail.com
Date2012-10-29 01:59 -0700
Message-ID<mailman.3000.1351501155.27098.python-list@python.org>
In reply to#32351
On Monday, October 29, 2012 1:38:04 AM UTC-7, Chris Rebert wrote:
> On Mon, Oct 29, 2012 at 1:24 AM, 
> 
> > On Sunday, October 28, 2012 9:44:56 PM UTC-7, alex23 wrote:
> 
> >> On Oct 29, 2:09 pm, Andrew < wrote:
> 
> You never wrapped `a` in a RangedSlicer or otherwise made use of RangedSlicer!
> 
> You wanted:
> 
> a = RangedSlicer([1,2,3,4,5])
> 
> 
> 
> >>>> a.__getitem__( slice(1,5) )
> 
> > [2, 3, 4, 5]
> 
> >
> 
> > Very odd...  I would have expected [1,2,3,4]
> 
> 
> 
> "[2, 3, 4, 5]" is the return value from `a.__getitem__( slice(1,5) )`
> 
> (or, equivalently, from `[1,2,3,4,5][1:5]`). It is not the result of
> 
> "print item"; that line of code is never executed since you never used
> 
> the RangedSlicer class at all.
> 
> 
> 
> Regards,
> 
> Chris

My apology --- I deleted that post; yet it didn't delete... I saw my mistake seconds after posting.

***** gmail.

Note: I subscribed to the python-list, and am able to recieve e-mails, but I don't see how to write a post for this particular thread nor subscribe to this particular thread...

A brief suggestion, or link to a howto would be *much* appreciated.

[toc] | [prev] | [next] | [standalone]


#32341

FromPaul Rubin <no.email@nospam.invalid>
Date2012-10-28 22:14 -0700
Message-ID<7x7gq96esk.fsf@ruckus.brouhaha.com>
In reply to#32335
Andrew <andrewr3mail@gmail.com> writes:
> So: Why does python choose to convert them to positive indexes, and
> have slice operate differently than xrange 

There was a thread a few years back, I think started by Bryan Olson,
that made the case that slice indexing is a Python wart for further
reasons than the above, and suggesting a notation like x[$-5] to denote
what we now call x[-5] (i.e. $ is the length of the string).  So your
example x[$-4:3] would clearly be the same thing as x[6:3] and not give
any suggestion that it might wrap around.

[toc] | [prev] | [next] | [standalone]


#32347

Fromandrewr3mail@gmail.com
Date2012-10-29 01:08 -0700
Message-ID<73d3b1ce-dde3-47d5-adb4-2beb5537598a@googlegroups.com>
In reply to#32341
On Sunday, October 28, 2012 10:14:03 PM UTC-7, Paul Rubin wrote:
> Andrew writes:
> 
> > So: Why does python choose to convert them to positive indexes, and
> 
> > have slice operate differently than xrange 
> 
> 
> 
> There was a thread a few years back, I think started by Bryan Olson,
> 
> that made the case that slice indexing is a Python wart for further
> 
> reasons than the above, and suggesting a notation like x[$-5] to denote
> 
> what we now call x[-5] (i.e. $ is the length of the string).  So your
> 
> example x[$-4:3] would clearly be the same thing as x[6:3] and not give
> 
> any suggestion that it might wrap around.

I'm getting very frustrated with the editor provided for this group... It keeps posting prematurely, and putting my email in even when I tell it not to each time; and there is no way to edit a post... but deleting is ok...

I think Olson makes a good point.  The len() operator is so ubiquitous that it would be very useful to have a shorthand like that.

I'll have to look for his thread.

I'm thinking that I might just patch my version of Python 3.x, in C, to allow iterators to be passed to __getitem__; I haven't ever seen someone wanting to use mixed sign indexes to extract a small chunk of an array in the middle; so I don't think my patch will break existing code.

The snippets of code given by other posters in the thread might also be used to make a compatibility wrapper; I'll have to study it closer; so that distributed code would still work on unpatched python, albeit much slower.

[toc] | [prev] | [next] | [standalone]


#32350

FromChris Rebert <clp2@rebertia.com>
Date2012-10-29 01:26 -0700
Message-ID<mailman.2998.1351499222.27098.python-list@python.org>
In reply to#32347
On Mon, Oct 29, 2012 at 1:08 AM,  <andrewr3mail@gmail.com> wrote:
> On Sunday, October 28, 2012 10:14:03 PM UTC-7, Paul Rubin wrote:
>> Andrew writes:
<snip>
> I'm getting very frustrated with the editor provided for this group... It keeps posting prematurely, and putting my email in even when I tell it not to each time; and there is no way to edit a post... but deleting is ok...

This is a Usenet newsgroup[1], not a web forum. There are noteworthy
differences between the two.
FWICT, you happen to be accessing us via Google Groups, which is
widely acknowledged to suck. We are not hosted *by* Google Groups;
they just happen to carry our posts.
Personally, I'd suggest using our mailing list mirror instead:
http://mail.python.org/mailman/listinfo/python-list
Or use some other, better newsgroup provider that also carries us.

[1]: http://en.wikipedia.org/wiki/Usenet

Regards,
Chris

[toc] | [prev] | [next] | [standalone]


#32336

FromAndrew <andrewr3mail@gmail.com>
Date2012-10-28 21:09 -0700
Message-ID<mailman.2989.1351483805.27098.python-list@python.org>
In reply to#32331
On Sunday, October 28, 2012 8:43:30 PM UTC-7, Ian wrote:
> On Sun, Oct 28, 2012 at 9:12 PM,  <Andrew> 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]".

Hi Ian,
Well, no it really isn't equivalent; although Python implements it as equivalent.

Consider a programmer who writes:
xrange(-4,3) 

They clearly *want* [-4,-3,-2,-1,0,1,2]

That is the "idea" of a range; So, for what reason would anyone want -4 to +3 to be 6:3???  Can you show me some code where this is desirable??

I do agree that the data held in -4 is equivalent to the data in 6, but the index is not the same.

So: Why does python choose to convert them to positive indexes, and have slice operate differently than xrange -- for the slice() object can't possibly know the size of the array when it is passed in to __getitem__;  They are totally separate classes.

I realize I can concat. two slice ranges, BUT, the ranges do not always span from negative to positive.

eg: a line in my program reads:
a[x-5:x]

if x is 7, then this is a positive index to a positive index.
So, there is no logic to using two slices concatd !

I use this arbitrary range code *often* so I need a general purpose solution.
I looked up slice() but the help is of no use, I don't even know how I might overload it to embed some logic to concatenate ranges of data; nor even if it is possible. 

[toc] | [prev] | [next] | [standalone]


#32332

FromMRAB <python@mrabarnett.plus.com>
Date2012-10-29 03:45 +0000
Message-ID<mailman.2987.1351482351.27098.python-list@python.org>
In reply to#32329
On 2012-10-29 03:12, andrewr3mail@gmail.com 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 [].
> }}}
>
If the stride is positive (if omitted it defaults to 1), the slice is
from the start index to one before the end index, and a negative index
counts from the end.

a[-4:3] is equivalent to a[len(a)-4:3], which is an empty list if
len(a)-4 >= 3.

It doesn't wrap around.

[toc] | [prev] | [next] | [standalone]


#32585

From88888 Dihedral <dihedral88888@googlemail.com>
Date2012-11-01 18:08 -0700
Message-ID<a4382f8a-c331-4a27-8450-9ec3e0f5ed24@googlegroups.com>
In reply to#32329
andrew...@gmail.com於 2012年10月29日星期一UTC+8上午11時12分11秒寫道:
> 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 [].
> 
> }}}
I'll suggest to use the reverse method
to get what you want.

Of course, the reverse method is not efficient for 
a list of a huge number of objects in python.

[toc] | [prev] | [standalone]


Page 4 of 4 — ← Prev page 1 2 3 [4]

Back to top | Article view | comp.lang.python


csiph-web