Slicing in Python 2.3a2
Sébastien Keim
s.keim at laposte.net
Tue Apr 1 07:41:35 EST 2003
I have some troubles to understand how work slicing in python 2.3.
In my examples, I will use the following list:
>>> A = range(10)
Python 2.3 add a new argument to slice wich can for sample be used to
get only the items at positive indexes:
>>> A[::2]
[0, 2, 4, 6, 8]
This works also for subslices: A[x:y:z] is equal to A[x:y][::z]
>>> A[1::2]
[1, 3, 5, 7, 9]
With a negative value you can reverse a list:
>>> A[::-1]
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
But I don't understand the result for a subslice with a negative third
argument:
>>> A[1::-1]
[1, 0]
Intuitively, I expected to get:
>>> A[1:][::-1]
[9, 8, 7, 6, 5, 4, 3, 2, 1]
The numarray package has the same behavior, so I guess it's intentional.
>>> import numarray
>>> numarray.array(range(10))[1::-1]
array([1, 0])
Then what is the real semantic for this third argument?
More information about the Python-list
mailing list