slice with negative stride

A

ajcppmod

I'm really confused about results of slices with negative strides. For
example

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
 
P

Paul Hankin

I'm really confused about results of slices with negative strides. For
example
...
Can someone explain to me how mystr[:3:-1] = 'gnirt'?

You've omitted the first number in the slice: python uses a sensible
default for it - in this case the index of the end of the string
because your stride is negative. If your stride was positive, the
sensible default is the start of the string, so mystr[:3:1] means
mystr[0:3:1] as you would expect.

It's explained in the python documentation, http://docs.python.org/lib/typesseq.html
(especially note 5 on that page).
 
F

Fredrik Lundh

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>
 
M

Mark T

I'm really confused about results of slices with negative strides. For
example

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
 

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,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top