question about slicing with a step length
Steven Bethard
steven.bethard at gmail.com
Thu Mar 9 12:39:59 EST 2006
John Salerno wrote:
> Given:
>
> numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>
> can someone explain to me why
>
> numbers[10:0:-2] results in [10, 8, 6, 4, 2]?
I've filed a bug report:
http://bugs.python.org/1446619
I suggest the following rewording for extended slices:
"""
To get the slice of s from i to j with step k, first
determine the bounds. If k is positive, and i or j is
greater than len(s), use len(s). If k is negative, and
i or j is greater than len(s)-1, use len(s)-1. Note, k
cannot be zero. If i or j are omitted then they become
``end'' values (which end depends on the sign of k).
The slice of s from i to j with step k is then defined
as the sequence of items with index x = i + n*k such
that 0 <= n < (j - i)/k. In other words, the indices
are i, i+k, i+2*k, i+3*k and so on, stopping when j is
reached (but never including j).
"""
I believe that properly explains the behavior, but if I've messed it up
(entirely likely), please suggest alternate wording on the bug report.
STeVe
More information about the Python-list
mailing list