[Edu-sig] Re: Confusion with slice notation

Christopher A. Craig com-nospam at ccraig.org
Mon Jan 5 14:11:30 EST 2004


"Douglas S. Blank" <dblank at brynmawr.edu> writes:

> I don't think it is the slice or the index notation that confuses many, but
> the fact that the negative numbers are distances to a line outside the array,
> and positive numbers are distances to the first element in the array. Or,
> negative numbers are distances, and positive numbers are positions.

Or they are both the position of the start of the element from the
start or end of the list.  Which is why I like my way of doing
things.  If we overlay 6 1cm items on a ruler and number in
centimeters from the start and from the end we have something like
this:

ItemA ItemB ItemC ItemD ItemE ItemF
|     |     |     |     |     |     |
0     1cm   2cm   3cm   4cm   5cm   6cm
-6cm  -5cm  -4cm  -3cm  -2cm  -1cm  -0

Then with positive indexes the first three items would be the
items between 0cm and 3cm ([0:3]) and with negative they would be the
items between -6cm and -3cm ([-6:-3]).

I think most people are used to saying that things reside at their
starting point, so it's reasonable to say that ItemF is at -5cm ([5])
or -1cm ([-1]) but [-1:-1] isn't anything, because there isn't
anything betweeen -1cm and -1cm.

-- 
Christopher A. Craig <com-nospam at ccraig.org>
"In the beginning the Universe was created.  This has made a lot of people
very angry and been widely regarded as a bad move." Douglas Adams




More information about the Edu-sig mailing list