Extended slices and indices

G

GavinCrooks

The indices method of slice doesn't seem to work quite how I would
expect when reversing a sequence.

For example :
s = '01234'
s[::-1] '43210'
s[slice(None,None,-1) ]
'43210'

So a slice with a negative step (and nothing else) reverses the
sequence. But what are the
corresponding indices?(4, -1, -1)

That looks O.K. The start is the last item in the sequence, and the
stop is one before the beginning of the sequence. But these indices
don't reverse the string:
''

Although they give the correct range:[4, 3, 2, 1, 0]

It would appear that there is no set of indices that will both reverse
the string and produce the correct range!

Is this a bug or a feature?

GEC

See also: http://www.python.org/doc/2.3.5/whatsnew/section-slices.html
 
R

Robert Kern

The indices method of slice doesn't seem to work quite how I would
expect when reversing a sequence.

For example :
s = '01234'
s[::-1] '43210'
s[slice(None,None,-1) ]
'43210'

So a slice with a negative step (and nothing else) reverses the
sequence. But what are the
corresponding indices?(4, -1, -1)

That looks O.K. The start is the last item in the sequence, and the
stop is one before the beginning of the sequence. But these indices
don't reverse the string:
''

Although they give the correct range:[4, 3, 2, 1, 0]

It would appear that there is no set of indices that will both reverse
the string and produce the correct range!

Is this a bug or a feature?

I'd say bug in the .indices() method. The meaning of [4:-1:-1] is unavoidable
different than [::-1] since the index -1 points to the last element, not the
imaginary element before the first element. Unfortunately, there *is* no
concrete (start, stop, step) tuple that will emulate [::-1].

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 
G

GavinCrooks

Robert said:
I'd say bug in the .indices() method. The meaning of [4:-1:-1] is unavoidable
different than [::-1] since the index -1 points to the last element, not the
imaginary element before the first element. Unfortunately, there *is* no
concrete (start, stop, step) tuple that will emulate [::-1].

After some more experimenting, it seems that [L-1:-L-1:-1] will reverse
a sequence of length L. But slice(L-1,-L-1,-1).indices(L) gives (L-1,
-1,-1) which will not reverse the sequence. And range(L-1, -L-1, -1) is
totally off, but range(L-1,-1,-1) is correct.

Seems like a bug (or an odd feature) of extended slicing of strings and
other built in sequences.

GEC
 
R

Robert Kern

Robert said:
I'd say bug in the .indices() method. The meaning of [4:-1:-1] is unavoidable
different than [::-1] since the index -1 points to the last element, not the
imaginary element before the first element. Unfortunately, there *is* no
concrete (start, stop, step) tuple that will emulate [::-1].

After some more experimenting, it seems that [L-1:-L-1:-1] will reverse
a sequence of length L.

Ah, yes. Good point.
But slice(L-1,-L-1,-1).indices(L) gives (L-1,
-1,-1) which will not reverse the sequence. And range(L-1, -L-1, -1) is
totally off, but range(L-1,-1,-1) is correct.

Seems like a bug (or an odd feature) of extended slicing of strings and
other built in sequences.

It's not a bug with extended slicing. -1 has a very definite meaning when used
as an index. The result of applying [4:-1:-1] is completely consistent with that
meaning. The problem is with .indices() for giving you something that is
inconsistent with that meaning. range() is neither here nor there; it's
semantics are simply different.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top