negative stride list slices

Russell Blau russblau at hotmail.com
Wed Sep 1 17:41:16 EDT 2004


"Shalabh Chaturvedi" <shalabh at cafepy.com> wrote in message
news:mailman.2735.1094073280.5135.python-list at python.org...
> Reid Nichol wrote:
> > This was the part that I was refering to:
> >
> >  +---+---+---+---+---+
> >  | H | e | l | p | A |
> >  +---+---+---+---+---+
> >  0   1   2   3   4   5
> > -5  -4  -3  -2  -1
> >
> >
> > Does it not all work the same in practice?
>
>
> I'd like to add:
>
>    +---+---+---+---+---+
>    | H | e | l | p | A |
>    +---+---+---+---+---+
>    0   1   2   3   4   5
>   -5  -4  -3  -2  -1
>    |                   |
>   start               end   (defaults for +ve step)
>   end                 start (defaults for -ve step)
>
> (Is this correct?)

The tutorial says, "The slice from i to j consists of all characters between
the edges labeled i and j, respectively."  So [1:3] starts at the left edge
of the character with index 1, and ends at the left edge of the character
with index 3, returning "el".  However, a slice from 3:1:-1 starts at the
*right* edge of the character with index 3, and ends at the *right* edge of
the character with index 1, returning "pl".  So the above diagram is not
correct for negative steps (the "backwards" slice consists of characters
between the edges labeled j+1 and i+1, in reverse order).

Is this difference intended, or a bug?  (One consequence of this difference
is that a slice of [5:0:-1] returns something different from a slice of [5:
:-1]!)

-- 
I don't actually read my hotmail account, but you can replace hotmail with
excite if you really want to reach me.





More information about the Python-list mailing list