with open('gaze.csv', 'r') as gaze: gaze_1_x, gaze_0_x, gaze_1_y, gaze_0_y, gaze_1_z, gaze_0_z = tuple(cols) for i in range(len(gaze_0_x)): x = float(gaze_1_x[i]) - float(gaze_0_x[i]) y = float(gaze_1_y[i]) - float(gaze_0_y[i]) z = float(gaze_1_z[i]) - float(gaze_0_z[i]) for line in gaze.readlines(): g = lambda x, y, z: ((x**2 + y**2 + z**2)**0.5, [x, y, z]) print(g) 

In summary: function lambda at 0x0DFA3A08 for each line of the file. What am I doing wrong?

ZY you need to substitute the values ​​from each line into the function and get the original answer to each line. When playing with code, the maximum was an insane addition-multiplication of the arguments of all the columns with their subsequent substitution.

  • And shouldn't lambda automatically return a value? - Enn Ilves
  • But why create lambda at every iteration? you just need to do ((x**2 + y**2 + z**2)**0.5 ? Then simply: g = (x**2 + y**2 + z**2)**0.5 Lambda will return the value if it is called () : (lambda x, y, z: (x**2 + y**2 + z**2)**0.5)(x, y, z) - gil9red
  • In this case, it turns out only one digit, which is displayed on each line, but not different values, what is the problem. : from - Enn Ilves
  • Well so, as written, and received. Because I did not write the desired result and input lines in the question, I have to play riddles with you :) I will try to guess again: g = (x**2 + y**2 + z**2)**0.5, [x, y, z] :) - gil9red
  • Csv gaze the 0.245462.0.291718, -0.924472 I wrote about what I want to get - a solution for the formula line by line, but I did not specify the format of the introductory lines. I open the file, make tuples in columns, and then spend with them operations to calculate x, y, z for each row, and then I want to apply x, y, z from each row in the formula to find a unique solution for each. - Enn Ilves

1 answer 1

Try the Pandas and Numpy modules :

 import numpy as np import pandas as pd # read CSV into a pandas.DataFrame df = pd.read_csv(r'D:\temp\.data\784492.csv') # calculate euclidean distance and save it as a new DF column df['dist'] = np.linalg.norm(df.filter(like='gaze_1').values - df.filter(like='gaze_0').values, axis=1) 

Result:

 In [65]: df Out[65]: gaze_0_x gaze_0_y gaze_0_z gaze_1_x gaze_1_y gaze_1_z dist 0 0.294526 0.302667 -0.906447 -0.244304 0.305290 -0.920388 0.539017 1 0.300238 0.358409 -0.883968 -0.261850 0.359709 -0.895569 0.562209 2 0.293682 0.298443 -0.908120 -0.245462 0.291718 -0.924472 0.539434 

PS Numpy / Pandas / scipy / sklearn modules allow you to perform vectorized calculations, which is usually orders of magnitude faster than solutions using (nested) cycles. In addition, the code looks much shorter and clearer (after a short introduction) ...

PPS problem in your code is that you print a pointer to a lambda function, instead of the result of a function call with parameters:

 print(g(x,y,z)) 

Example:

 In [71]: f = lambda a,b: max(a,b) In [72]: print(f) <function <lambda> at 0x000000000B748F28> In [73]: print(f(1,2)) 2 
  • Oh, it's you again. Thanks for the help again. c: Actually, a bit silly of me, but I still continued to use the default csv module, but now I see that it is worth dropping it. It really looks human. - Enn Ilves