on slices, negative indices, which are the equivalent procedures?

Chris Angelico rosuav at gmail.com
Mon Aug 9 17:57:40 EDT 2021


On Tue, Aug 10, 2021 at 7:24 AM Jack Brandom <jbrandom at example.com> wrote:
>
> Greg Ewing <greg.ewing at canterbury.ac.nz> writes:
>
> > On 6/08/21 12:00 pm, Jack Brandom wrote:
> >> It seems
> >> that I'd begin at position 3 (that's "k" which I save somewhere), then I
> >> subtract 1 from 3, getting 2 (that's "c", which I save somewhere), then
> >> I subtract 1 from 2, getting 1 (that's "a", ...), then I subtract 1 from
> >> 1, getting 0 (that's J, ...), so I got "kcaJ" but my counter is 0 not
> >> -13, which was my stopping point.
> >
> > You need to first replace any negative or missing indices with
> > equivalent indices measured from the start of the string.
> >
> > When you do that in this example, you end up iterating backwards from 3
> > and stopping at -1.
>
> Yeah, that makes sense now.  But it sucks that the rule for replacing
> negative indices is sometimes missing index and sometimes positive
> index.  (That is, we can't always use positive indices.  Sometimes we
> must use no index at all.  I mean that's how it looks to my eyes.)

Sometimes, the index  you need to use is the value None. You cannot
use a positive number to indicate the position to the left of zero -
at least, not if you consider numbers to be on a number line.

ChrisA


More information about the Python-list mailing list