Slice confusion : a[n:p] is a list exclude the last element p

Paddy McCarthy paddy3118 at netscape.net
Mon Apr 28 17:02:19 EDT 2003


Alex Martelli <aleax at aleax.it> wrote in message news:<UXara.22428$K35.745468 at news2.tin.it>...
<<SNIP>>
> For example, for all XX, x, y, z, XX[x:y]+XX[y:z] == XX[x:z].  This
> kind of regularity is exactly what facilitates reasoning about
> programs, and in particular modifying programs.
> 

For x<=y<=z surely.

<<SNIP>>
> Alex

<example>

>>> XX=range(10)
>>> XX
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> x,y,z = 1,4,7
>>> print XX[x:y] +XX[y:z], XX[x:z]
[1, 2, 3, 4, 5, 6] [1, 2, 3, 4, 5, 6]
>>> y
4
>>> y = -2
>>> print XX[x:y] +XX[y:z], XX[x:z]
[1, 2, 3, 4, 5, 6, 7] [1, 2, 3, 4, 5, 6]
>>> XX[y:z]
[]
>>> 

</example>

But I agree with Alex, it turned out to be less error prone for me if
I adopted the inclusive lower bound, non-inclusive upper bound way of
doing things.
It may look odd at first but you get used to it and the array bounds
maths I find easier.

Cheers, Paddy.




More information about the Python-list mailing list