[Numpy-discussion] Can I index array starting with 1?

Derek Homeier derek at astro.physik.uni-goettingen.de
Thu Jul 28 20:25:51 EDT 2011


On 29.07.2011, at 1:38AM, Anne Archibald wrote:

> The can is open and the worms are everywhere, so:
> 
> The big problem with one-based indexing for numpy is interpretation.
> In python indexing, -1 is the last element of the array, and ranges
> have a specific meaning. In a hypothetical one-based indexing scheme,
> would the last element be element 0? if not, what does looking up zero
> do? What about ranges - do ranges still include the first endpoint and
> not the second? I suppose one could choose the most pythonic of the
> 1-based conventions, but do any of them provide from-the-end indexing
> without special syntax?
> 
I forgot, this definitely needs to be preserved for ndarray!

> Once one had decided what to do, implementation would be pretty easy -
> just make a subclass of ndarray that replaces the indexing function.

In fact, Stéfan's reshuffling trick does nearly everything I would expect for using negative indices, maybe the only functionality needed to implement is 
1. define an attribute like x.start that could tell appropriate functions (e.g. for print(x) or plot(x)) the "zero-point", so x would be evaluated e.g. at x[-5], wrapping around at [x-1], x[0] to x[-6]... Should have the advantage that anything that's not yet aware of this attribute could simply ignore it. 
2. allow to automatically set this starting point when creating something like "x = np.zeros(-5:7)" or setting a shape to (-5:7) - but maybe the latter is leading into very dangerous territory already...

Cheers,
							Derek




More information about the NumPy-Discussion mailing list