[Numpy-discussion] Question about bounds checking

Rich E rjel at ceh.ac.uk
Mon Aug 10 11:08:27 EDT 2009


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

 





More information about the NumPy-Discussion mailing list