[Numpy-discussion] Question about bounds checking

Chris Colbert sccolbert@gmail....
Mon Aug 10 10:44:23 CDT 2009


when you use slice notation, [0:4] returns everything up-to but not
including index 4. That is a[4] is actually the 5th element of the
array (which doesn't exist) because arrays are zero-based in python.

http://docs.scipy.org/doc/numpy-1.3.x/user/basics.indexing.html


On Mon, Aug 10, 2009 at 11:08 AM, Rich E<rjel@ceh.ac.uk> wrote:
> Dear all,
> I am having a few issues with indexing in numpy and wondered if you could help
> me out.
> If I define an array
> a = zeros(( 4))
> a
> array([ 0.,  0.,  0.,  0.])
>
> Then I try and reference a point beyond the bounds of the array
>
> a[4]
> Traceback (most recent call last):
>  File "<stdin>", line 1, in <module>
> IndexError: index out of bounds
>
> but if I use the slicing format to reference the point I get
>
> a[0:4]
> array([ 0.,  0.,  0.,  0.])
> a[0:10]
> array([ 0.,  0.,  0.,  0.])
>
> it returns a[ 0 : 3 ], with no error raised. If I then ask for the shape of the
> array, I get
> a.shape
> (4,)
>
> but if I use
>
> a[0:3].shape
> (3,)
>
> which is one less than I would have expected.
>
> a[0:4].shape
> (4,)
>
> This is numpy 1.2.1 on python 2.5
> Thanks in advance for you help,
> Rich
>
>
>
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>


More information about the NumPy-Discussion mailing list