Possible improvement to slice opperations.

Michael Hudson mwh at python.net
Fri Sep 9 10:30:02 CEST 2005


Ron Adam <rrr at ronadam.com> writes:

> Magnus Lycka wrote:
>> Ron Adam wrote:
[...]
>>> REVERSE ORDER STEPPING
>>> ----------------------
>>> When negative steps are used, a slice operation
>>> does the following.  (or the equivalent)
>>>
>>>    1. reverse the list
>>>    2. cut the reversed sequence using start and stop
>>>    3. iterate forward using the absolute value of step.
>> I think you are looking at this from the wrong perspective.
>> Whatever sign c has:
>> For s[a:b:c], a is the index for the first item to include,
>> b is the item after the last to include (just like .end() in
>> C++ iterators for instance), and c describes the step size.
>
> Yes, and that is how it "should" work.  But....
>
> With current slicing and a negative step...
>
> [   1   2   3   4   5   6   7   8   9  ]
>   -9  -8  -7  -6  -5  -4  -3  -2  -1  -0
>
> r[-3:]  ->  [7, 8, 9]    # as expected
> r[-3::-1] ->  [7, 6, 5, 4, 3, 2, 1, 0]   # surprise
>
> The seven is include in both cases, so it's not a true inverse
> selection either.

Did you read what Magnus said: "a is the index for the first item to
include"?  How could r[-3::x] for any x not include the 7?

Cheers,
mwh

-- 
  Windows 2000: Smaller cow. Just as much crap.
                           -- Jim's pedigree of operating systems, asr



More information about the Python-list mailing list