473,508 Members | 3,343 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

slice with negative stride

I'm really confused about results of slices with negative strides. For
example
>>mystr = 'my string'
I would have then thought of the contents of mystr as:

indices 0 1 2 3 4 5 6 7 8
content m y s t r i n g

with mystr[:3] = 'my '

Can someone explain to me how mystr[:3:-1] = 'gnirt'?

I was expecting the result to be mystr[:3] reversed (' ym') i.e slice
then reverse or even the first 3 elements of the string after being
reversed ('gni') i.e. reverse then slice.

Thanks

Andy

Oct 6 '07 #1
2 3870

<aj******@gmail.comwrote in message
news:11*********************@o3g2000hsb.googlegrou ps.com...
I'm really confused about results of slices with negative strides. For
example
>>>mystr = 'my string'

I would have then thought of the contents of mystr as:

indices 0 1 2 3 4 5 6 7 8
content m y s t r i n g

with mystr[:3] = 'my '

Can someone explain to me how mystr[:3:-1] = 'gnirt'?

I was expecting the result to be mystr[:3] reversed (' ym') i.e slice
then reverse or even the first 3 elements of the string after being
reversed ('gni') i.e. reverse then slice.

Thanks

Andy
When the step is negative, a missing start is interpreted as the end of the
string. A slice always includes the start index character through, but not
including, the end index character. In your example, the end index
character was mystr[3], so you received the end of the string ('g') down to
but not including 's', which is 'gnirt'.

To see the indices a slice is using, use the slice object's indices method.
Given the length of a string, it returns the exact start,stop,step indices
used:
>>mystr='my string'
s=slice(None,3,-1)
s.indices(len(mystr)) # start is the end of the string if step is
negative
(8, 3, -1)
>>mystr[8],mystr[3]
('g', 's')
>>mystr[8:3:-1]
'gnirt'
>>s=slice(None,3,1)
s.indices(len(mystr)) # start is the beginning of the string if step is
positive
(0, 3, 1)
>>mystr[0],mystr[3]
('m', 's')
>>mystr[0:3:1]
'my '

-Mark T
Oct 6 '07 #2
aj******@gmail.com wrote:
>>mystr = 'my string'

I would have then thought of the contents of mystr as:

indices 0 1 2 3 4 5 6 7 8
content m y s t r i n g

with mystr[:3] = 'my '

Can someone explain to me how mystr[:3:-1] = 'gnirt'?
A slice [i:j:k] includes the first index (i) but *not* the last index
(j). Since you're stepping backwards, the slice will start at the end
of the string (i=len(mystr)-1=8) and stop when it reaches j=3.
>>mystr[8]
'g'
>>mystr[7]
'n'
>>mystr[6]
'i'
>>mystr[5]
'r'
>>mystr[4]
't'

</F>

Oct 6 '07 #3

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

Similar topics

15
2466
by: Roberto A. F. De Almeida | last post by:
I found that when using negative indices, the slice object passed to __getitem__ depends on the number of slices. An example to clarify: class a: def __getitem__(self, index): return index ...
2
1748
by: Uwe Mayer | last post by:
Hi, a class of mine should support the list interface and implements the __len__ and __getitem__ methods. Now when I ask for an unbounded slice: >>> len( myObj ) my __getitem__(self, y)...
19
2568
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...
108
6298
by: Bryan Olson | last post by:
The Python slice type has one method 'indices', and reportedly: This method takes a single integer argument /length/ and computes information about the extended slice that the slice object would...
40
2565
by: Ron Adam | last post by:
After considering several alternatives and trying out a few ideas with a modified list object Bengt Richter posted, (Thank You), I think I've found a way to make slice operation (especially far end...
1
1519
by: 700MHz | last post by:
I cannot quite understand when the third index is a negative number,like this: a = '0123456789' a I know the index step is 2, so it will collect items from offset 1, 3, 5, 7, 9 but when a...
0
1577
by: Clemens Hintze | last post by:
Hello, I have a question concerning the usage of default constructed std::slice instances. Our company currently validate the GNU-G++ 3.4 compiler against the ISO/IEC 14882:2003 standard for...
11
4431
by: truckaxle | last post by:
I am trying to pass a slice from a larger 2-dimensional array to a function that will work on a smaller region of the array space. The code below is a distillation of what I am trying to...
0
7229
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7129
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
7333
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,...
0
7398
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...
1
5057
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
3208
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...
0
3194
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1566
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
769
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.