472,353 Members | 1,902 Online

# Extended slices and indices

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?
>>slice(None,None,-1).indices(len(s))
(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:
>>s[4:-1:-1]
''

Although they give the correct range:
>>range( 4, -1,-1)
[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

Sep 24 '06 #1
3 2019
Ga*********@gmail.com wrote:
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?
>>>slice(None,None,-1).indices(len(s))
(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:
>>>s[4:-1:-1]
''

Although they give the correct range:
>>>range( 4, -1,-1)
[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
an underlying truth."
-- Umberto Eco

Sep 24 '06 #2

Robert Kern wrote:
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

Sep 24 '06 #3
Ga*********@gmail.com wrote:
Robert Kern wrote:
>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
an underlying truth."
-- Umberto Eco

Sep 24 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

### Similar topics

 19 by: David Abrahams | last post by: Can anyone explain the logic behind the behavior of list slicing with negative strides? For example: >>> print range(10) I found this... 29 by: George Sakkis | last post by: Why does slicing a tuple returns a new tuple instead of a view of the existing one, given that tuples are immutable ? I ended up writing a custom... 1 by: Rodney Maxwell | last post by: The following are apparently legal Python syntactically: L L But they don't seem to work on lists: Traceback (most recent call last): File... 5 by: NuberSteve | last post by: I'm very new to using CSS and also the concept of slices for mouse-overs, and have made my first attempt at using ImageReady to generate slices of a... 0 by: Naresh1 | last post by: What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge... 0 by: jalbright99669 | last post by: Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made... 2 by: Matthew3360 | last post by: Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it... 0 by: Arjunsri | last post by: I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and... 0 by: WisdomUfot | last post by: It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific... 0 by: Matthew3360 | last post by: Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web... 0 by: Oralloy | last post by: Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the... 0 by: Carina712 | last post by: Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand.... 0 by: Rahul1995seven | last post by: Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...