Error

Dave Angel d at davea.name
Tue Nov 20 08:01:52 EST 2012


On 11/20/2012 07:31 AM, inshu chauhan wrote:
> I did the following changes in this part of my programme.. now the
> refereence error is removed but its showing me another error :
>
> def ComputeClasses(data):
>     radius = .5
>     points = []
>     for cy in xrange(0, data.height):
>         for cx in xrange(0, data.width):
>
>             if data[cy,cx] == (0.0,0.0,0.0):
>                 continue
>             else :
>                 centre = data[cy, cx]
>                 print centre
>                 points.append(centre)
>
>
>             change = True
>
>             while change:
>
>                 for ring_number in xrange(1, 1000):
>                     change = False
>                     new_indices = GenerateRing(cx, cy, ring_number)
>
>                     for idx in new_indices:
>                         point = data[idx[0], idx[1]]
>
>                         if point == (0.0, 0.0, 0.0 ):
>                           continue
>                         else:
>
>                             dist = distance(centre, point)
>                             if  dist < radius :
>                                 print point
>                                 points.append(point)
>                                 change = True
>                                 print change
>
>
>                 break
>
>
>             print points
>
>
> ERROR :
>
> Traceback (most recent call last):
>   File "Z:/modules/classification1.py", line 71, in <module>
>     ComputeClasses(data)
>   File "Z:/modules/classification1.py", line 47, in ComputeClasses
>     point = data[idx[0], idx[1]]
> error: index is out of range
>
> What is meant by this statement ' Index out of range ' ? Does it mean that
> my range 1, 1000 is exceeded ??
>
>

When you're using custom classes that mimic the standard ones, the error
can mean most anything.  But assuming the design was to keep as close as
possible, it simply means that you're subscripting a list with an index
that's too large or too small.  So if idx is a list that has only one
element, element number zero, then idx[1] would be out of range.  On the
same line, if data is acting kind of like a two-dimensional list, then
it has limits on each dimension, and either idx[0] is too big/small for
the first dimension, or idx[1] is too big or small for the second.

First thing is to figure out which part of this expression is causing
the exception.  So do a separate pair of assignments,
    dummy0 = idx[0]
    dummy1 = idx[1]

and then  point = data[dummy0, dummy1]

Incidentally, if idx is a tuple or a list, of exactly two items, then
you could just say
       point = data[*idx]

Anyway, if that still doesn't make things clear, then print dummy0 and
dummy1 before the point= line.  That way you can see the last value, the
one it dies on, just before the stack trace.  Naturally, you could also
print the size attributes of the data item as well.


-- 

DaveA




More information about the Python-list mailing list