a prob.. error in prog ..dont knw how to correct

Jussi Piitulainen jpiitula at ling.helsinki.fi
Sun Oct 21 07:55:31 EDT 2012


inshu chauhan writes:

> I am new to python and have a little problem to solve .. i have an
> array with x, y, z co-ordinates in it as a tuple. I am trying to find
> the distance between each point and sorting the points according to
> the min distance.. i have tried a prog but m stuck bcoz of this error
> which I am unable to correct

You don't need help with your program. You need to learn debugging.
Your program is excellent material for that.

The first main step is to learn how to launch an interactive Python
session. Type in some expressions and learn how the interpreter
responds. The second main step is to learn how to send/import your
program to such an interactive Python session. Then you can call your
own functions on your own data and the interpreter's responses will
give you information about your own program.

> import cv
> from math import floor, sqrt, ceil
> from numpy import array, dot, subtract, add, linalg as lin

Unused? Remove for the duration of the debugging exercise.

> def calcdist(data):
>     for p in data:
>         x = p[0]
>         y = p[1]
>         z = p[2]
>     for i in range(len(data)):
>       dist = sqrt((x[i]-x[i+1])**2 + (y[i]-y[i+1])**2 +(z[i]-z[i+1]**2))
>       return dist

This is the one you need to call in your debugging session. When you
get that far, consider also adding temporary print statements to show
what the function is doing.

> def ReadPointCloud(filename):
>     return [tuple(map(float, l.split()[1:4])) for l in open(filename)]
>
> def main (data):
> 
>     for i in range(len(data)): # Finding Neighbours
>        for j in range(len(data)):
>           dist = calcdist(data)
>           print dist
 
Well done separating the main routine as a function - you can also
call this in the interpreter.

> if __name__ == '__main__':
>     data = ReadPointCloud(r'C:\Thesis\NEHreflectance_Scanner_1_part.txt')
>     data = data[0:100]
>     main(data)

Consider using a very small constant data set in the script while you
are debugging - one, two, three points. (When you get the program
working, make the file name a command line parameter, but that can
wait.)

> the error m getting is...
> 
> Traceback (most recent call last):
>   File "C:\Users\inshu\Desktop\cal-dist.py", line 29, in <module>
>     main(data)
>   File "C:\Users\inshu\Desktop\cal-dist.py", line 22, in main
>     dist = calcdist(data)
>   File "C:\Users\inshu\Desktop\cal-dist.py", line 11, in calcdist
>     dist = sqrt((x[i]-x[i+1])**2 + (y[i]-y[i+1])**2 +(z[i]-z[i+1]**2))
> TypeError: 'float' object has no attribute '__getitem__'

Python version would be useful information, along with the information
on how you launch your program. (I don't know Microsoft environments,
but other people here do.)

Consider renaming your program as caldist.py, without the hyphen. I
think Python will complain about invalid syntax if you try to import a
module named cal-dist.

I get a different message when I try to index a float, saying
"unsubscriptable" or "not subcscriptable". Regardless, it seems that
your program is asking for x[i] where x is a number, and Python cannot
make sense of that.

(A 'float' is a type of number that lives in a computer and behaves
just a little bit like the real numbers of mathematics.)



More information about the Python-list mailing list