Slice objects with negative increment
Paul Hughett
hughett at mercur.uphs.upenn.edu
Wed May 1 13:59:58 EDT 2002
Alex Martelli <aleax at aleax.it> wrote:
: Paul Hughett wrote:
: ...
:> Hmm. Is the value of r[-1:-1:-1] equal to array([12, 11, 10, 9, 8, 7,
:> 6, 5, 4, 3, 2, 1, 0]) or to an empty array? It could plausibly be
: Any slice x[a:a] is empty. Why ever should x[a:a:-1] be any different
: from x[a:a:1]?! I suggest you download and install Numeric so you
: can easily check on such doubts.
: Slicing rules are simple. [details omitted]
Perhaps, but they give some results that I find anomalous. Consider this
mini-problem: Given i and j such that j > i >= 0, write a snippet of
code to construct a slice object that selects elements j-1, j-2, ..., i.
If I understand the interpretation of slices correctly, i = 0 must
be treated as a special case and the code must look something like
if i == 0 :
slc = slice(j, None, -1)
else :
slc = slice(j, i, -1)
: Even having just one convention might be a problem if the convention
: was bad -- complicated, irregular, inconsistent. I think Python's
: arrangement here is quite good.
I'm less convinced of that, in light of the example just given.
Having to treat element zero as a special case could come as a nasty
surprise to some programmer.
Paul Hughett
More information about the Python-list
mailing list