Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #31843 > unrolled thread
| Started by | inshu chauhan <insideshoes@gmail.com> |
|---|---|
| First post | 2012-10-21 12:09 +0200 |
| Last post | 2012-10-21 14:55 +0300 |
| Articles | 2 — 2 participants |
Back to article view | Back to comp.lang.python
a prob.. error in prog ..dont knw how to correct inshu chauhan <insideshoes@gmail.com> - 2012-10-21 12:09 +0200
Re: a prob.. error in prog ..dont knw how to correct Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2012-10-21 14:55 +0300
| From | inshu chauhan <insideshoes@gmail.com> |
|---|---|
| Date | 2012-10-21 12:09 +0200 |
| Subject | a prob.. error in prog ..dont knw how to correct |
| Message-ID | <mailman.2583.1350814179.27098.python-list@python.org> |
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
import cv
from math import floor, sqrt, ceil
from numpy import array, dot, subtract, add, linalg as lin
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
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
if __name__ == '__main__':
data = ReadPointCloud(r'C:\Thesis\NEHreflectance_Scanner_1_part.txt')
data = data[0:100]
main(data)
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__'
[toc] | [next] | [standalone]
| From | Jussi Piitulainen <jpiitula@ling.helsinki.fi> |
|---|---|
| Date | 2012-10-21 14:55 +0300 |
| Message-ID | <qotmwzgca4s.fsf@ruuvi.it.helsinki.fi> |
| In reply to | #31843 |
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.)
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web