473,796 Members | 2,538 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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,No ne,-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 2120
Ga*********@gma il.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,N one,-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*********@gma il.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
2609
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 rules written down somewhere. Thanks,
29
3412
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 wonder why it is not implemented for tuples. George
1
2126
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 indices must be integers Traceback (most recent call last): File "<stdin>", line 1, in <module>
5
3838
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 show various countries appearing to be depressed when "moused-over". To keep it simple at first, I just decided to try two countries. After copying the HTML and JavaScript codes generated by ImageReady into the page I wanted to insert the map into,...
0
9530
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10459
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10236
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10182
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10017
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7552
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5445
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5577
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3734
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.