Slice objects with negative increment

Paul Hughett hughett at mercur.uphs.upenn.edu
Tue Apr 30 10:04:08 EDT 2002


Alex Martelli <aleax at aleax.it> wrote:
: Paul Hughett wrote:
:         ...
:> Hence my question: Is there an established meaning within Python for
:> slice objects with a negative increment?  E.g. slice(1, 10, -2) ?

: Sure!  [Use the same convention as Numeric]

Okay, I've looked up the convention for Numeric, but the description
isn't entirely clear in the case of negative increments.  It states that
the default values for start and stop when the increment is negative are
"the beginning" and "the end" of the axis.  Suppose that the axis has
n elements.  Does ::-k default to 

   n:0:-k        which refers to element n, which doesn't exist
   (n-1):0:-k    which skips element 0
   n:-1:-k       which refers to element n, and apparently to -1 => n-1, or
   (n-1):-1:-k   which is not generally the reversal of 0:n:k for k > 1


By the way, I don't see anything in "Numerical Python" about the
meaning of a negative start or stop.  Do they follow the usual Python
convention of meaning the nth element from the end?


Paul Hughett



More information about the Python-list mailing list