[Numpy-discussion] Does zero based indexing drive anyone else crazy?
Christopher Barker
Chris.Barker at noaa.gov
Mon Jul 3 14:42:14 EDT 2006
Webb Sprague wrote:
> it is far more natural to program if the indices are aligned with the
> counts of the elements
I suggest that it's only more natural if that's what you're used to --
i.e. you come form other languages that do it that way.
I fairly rarely get bitten by indexing 1, rather than zero, but I save a
lot of errors that I used to get in MATLAB by the way python does slices:
len(a[i:j]) == j - i
and:
l[:j] + l[j:] == l
or:
r_[a[:i],a[i:]] == a
for numpy arrays.
I suppose you could have one-indexing and the python slicing, but I
think that would be even more error prone.
> zero
> based indexing made lots of sense since it dovetailed with thinking in
> memory offsets in systems programming
it also dovetails nicely into using an array to represent a grid of values:
i = (X - MinX) / deltaX
rather than
i = (X - MinX) / deltaX + 1
X = i*deltaX
rather than
X = (i-1)*deltaX
In Fortran, you can choose where you want your array indexing to start,
and I found myself starting with zero more often than 1, and I was never
a C programmer.
> I guess I just want to vent, but also to ask if anyone
> has found any way to deal with this issue in their own scientific
> programming.
You'll get used to it. There are disadvantages either way, but after
switching from primarily Matlab to primarily Python, I like zero-based
indexing better. Perhaps you will too.
-Chris
--
Christopher Barker, Ph.D.
Oceanographer
NOAA/OR&R/HAZMAT (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception
Chris.Barker at noaa.gov
More information about the NumPy-Discussion
mailing list