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

Zero Piraeus schesis at gmail.com
Sun Oct 21 07:23:56 EDT 2012


:

On 21 October 2012 06:09, inshu chauhan <insideshoes at gmail.com> wrote:
> I am new to python and have a little problem to solve ..

> import cv

This module is not used in your code [and isn't part of the standard library].

> from math import floor, sqrt, ceil

You're only using one of these functions.

> from numpy import array, dot, subtract, add, linalg as lin

You're not using any of this.

>     for p in data:
>         x = p[0]
>         y = p[1]
>         z = p[2]

You're repeatedly making assignments in this loop, but because your
subsequent loop is not nested inside it, only the last iteration of x,
y, z will have anything done with them.

>     for i in range(len(data)):

Whenever you find yourself writing

  for i in range(len(seq)):

you're doing something wrong. Instead use something like

  for a, b, c in seq:

or, if you really need the index,

  for i, (a, b, c) in enumerate(seq):

>       dist = sqrt((x[i]-x[i+1])**2 + (y[i]-y[i+1])**2 +(z[i]-z[i+1]**2))

As previously noted, you're going to be repeatedly working with the
last values of x, y and z from the previous loop here. In addition,
since x, y and z are, according to your traceback, floats, trying to
access their members as though they are sequences isn't going to work.

>       return dist

This return statement is inside a loop, and will terminate the
function the first time it's called. That might not be what you want.

> def ReadPointCloud(filename):
>     return [tuple(map(float, l.split()[1:4])) for l in open(filename)]

Assuming your file is along these lines:

  p1 1.23 4.56 7.89
  p2 9.87 6.54 3.21

... ReadPointCloud() ought to work. However, it's not very readable -
map() is sometimes useful, but usually a list comprehension is
clearer. This is better:

def ReadPointCloud(filename):
    """Return a list of 3-tuples from ``filename``."""
    # not tested:
    points = []
    with open(filename) as f:
        for line in f:
            point = tuple(float(x) for x in l.split()[1:4])
            points.append(point)
    return points

> def main (data):
>
>     for i in range(len(data)): # Finding Neighbours
>        for j in range(len(data)):
>           dist = calcdist(data)
>           print dist

This will call calcdist() on everything in data, N**2 times [where N
is the number of points you're working with]. That's probably not what
you want.

> if __name__ == '__main__':
>     data = ReadPointCloud(r'C:\Thesis\NEHreflectance_Scanner_1_part.txt')

Except for throwaway programs, it's not a good idea to hard-code
filenames like this. Better:

if __name__ == '__main__':
    import sys
    data = ReadPointCloud(sys.argv[1])

In summary: copy-pasting code you don't understand and mashing it
together randomly is not generally considered an effective development
technique.

 -[]z.



More information about the Python-list mailing list