473,412 Members | 2,054 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

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

See also: http://www.python.org/doc/2.3.5/what...on-slices.html

Sep 24 '06 #1
3 2106
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
that is made terrible by our own mad attempt to interpret it as though it had
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
that is made terrible by our own mad attempt to interpret it as though it had
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 result very surprising, and would just like to see the...
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 ImmutableSequence class that does this, but I...
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 "<stdin>", line 1, in <module> TypeError: list...
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 world map. I basically wanted a map that would...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.