Possible improvement to slice opperations.

Ron Adam rrr at ronadam.com
Tue Sep 6 18:05:01 EDT 2005


Patrick Maupin wrote:
> Ron Adam wrote:
> 
> 
>>>This should never fail with an assertion error.  You will note that it
>>>shows that, for non-negative start and end values, slicing behavior is
>>>_exactly_ like extended range behavior.
> 
> 
>>Yes, and it passes for negative start and end values as well.
> 
> 
> Umm, no:
> 
> .>> for stride in [-3, -2, -1, 1, 2, 3]:
> ...     for start in range(-1,len(L)):
> ...         for end in range(-1,len(L)):
> ...             P = L[start:end:stride]
> ...             Q = [L[i] for i in range(start, end, stride)]
> ...             assert P==Q, [start, end, stride, P, Q]
> ...
> Traceback (most recent call last):
>   File "<stdin>", line 6, in ?
> AssertionError: [-1, 0, -3, [9, 6, 3], []]

Ah, Yes... I it was way too late last night and I mistakenly changed the 
values of L... which was meaningless.

Range lines in the for statements, need to read..

     range(-len(l),0)

But then it doesn't include all the values of L.


>>Thanks again, this pretty much explains why slices opperate the
>>way they do.  And it explains why the edge case's happen as well I think.
> 
> 
> You're welcome.
> 
> Regards,
> Pat
> 



More information about the Python-list mailing list