Index Error

Dave Angel d at davea.name
Tue Nov 20 11:17:47 EST 2012


On 11/20/2012 10:01 AM, inshu chauhan wrote:
>>
>>
>> So did you read the following paragraphs?  You should not be using "and"
>> in that expression.
> 
> 
> 
>> Yes i tried "or" also but no use .....

Explain "no use".  If you mean you still fail, then what else did you
try?  For example, did you try interchanging the two subscripts?  I've
suspected all along that the meanings of row and column, x and y, [0]
and [1], height and width are possibly confused.  Perhaps it'd be better
if you used a similar terminology everywhere.

Or perhaps you should simply make a class called Point, with attributes
x and y.  And use instances of that class rather than tuples.  And start
the function by defining  xmax and ymax, from  data.height and
data.width (in whatever order matches the docs of that library you're using)


>>
>>>>
>>>> You do not want "and" in that expression.  The way you've coded it,
>>>> it'll only skip items in which both indices are out of range.  Change
>> it to
>>>>                   if idx[0] >= data.width or idx[1] >= data.height:
>>>>
>>>> and depending on your answer to my earlier query, you may want to also
>>>> check if either subscript is negative.
>>>>
>>>>>                             continue
>>>>>                         else :
>>>>>                             point = data[idx[0], idx[1]]
>>>>>                             if point == (0.0, 0.0, 0.0 ):
>>>>>                                 print point
>>>>>                                 continue
>>>>>                             else:
>>>>>                                 dist = distance(centre, point)
>>>>>                                 print dist
>>>>>                                 if  dist < radius :               and
>>>> rings
>>>>> should be added only when this condition is satisfied
>>>>>                                     print point
>>>>>                                     points.append(point)
>>>>>                                     change = True
>>>>>                                     print change
>>>>>
>>>>>
>>>>>                         break
>>>>
>>>> Why do you want to terminate the loop after only iteration?
>>>>
>>>   <snip>
> The idea here is if no more point is added to the list of points i.e. all
> points surrounding the centre is zero.. I want to break the loop and go to
> the next point..
> 
That's not at all what the break does.  But it's probably not what you
meant to say anyway.
> 
I think what you're saying is that you want to append at most one of the
points from the ring.  In that case, the break is appropriate, but it'd
be much clearer if it were inside the clause that triggers it, the place
where you say points.append(point).  (naturally, it'd be at the end of
that clause, following print change.)  In other words indent it to line
up with print change.

Back to an earlier comment.  I asked if N was ever bigger than x or
bigger than y, and you said never.  But your ComputeClasses will have
such a case the very first time around, when cx==0, cy==0, and
ring_number == 1.

Have you actually tested a trivial nested loop:

   for cy in xrange(0, data.height):
        for cx in xrange(0, data.width):
	    point = data[cy, cx]

to see whether it blows up.  And if it does, whether reversing cy and cx
will change it?


Your comment in the line:
     if  dist < radius :    and rings should be added
                   only when this condition is satisfied

is confusing to me.  How can you make the call to GenerateRing() after
this test, when this test is measuring something about one of the values
returned by GenerateRing ?

I must confess I have no idea what data represents.  When you're doing
rings, you use deltas on the cx and cy values.  But when you're
computing radius, you use the 3d coordinates returned by  data[cx, cy].
 So is data some kind of transformation, like a projection from a 3d
object into a plane ?



-- 

DaveA



More information about the Python-list mailing list