-3

The points are

x=[[0.5697071,0.447144773,0.45310486]]

and

z=[[0,0.47144773043356025,0],[0,0.47144773043356025,0.4531048568095023],[0.5697070991026062,0.47144773043356025,0.4531048568095023],[0,0,0]]

I want a Python code to solve the above problem, I tried using unpacking of lists but I am getting a lot of errors.

Timus
  • 10,974
  • 5
  • 14
  • 28
sai
  • 1
  • 1

1 Answers1

0

Here's a way:

(EDIT: On @Timus's advice - sum switched to math.fsum to reduce loss of precision)

import math

def distance(point1, point2):
    return math.sqrt(math.fsum((dim2-dim1)**2 for dim1, dim2 in zip(point1, point2)))


x=[[0.5697071,0.447144773,0.45310486]]
z=[[0,0.47144773043356025,0],[0,0.47144773043356025,0.4531048568095023],[0.5697070991026062,0.47144773043356025,0.4531048568095023],[0,0,0]]


distances = [distance(each_z, x[0]) for each_z in z]

print (distances)
#result: [0.7283274179145305, 0.5702252305277516, 0.024302957433560483, 0.8542883833737007]
Vin
  • 929
  • 1
  • 7
  • 14
  • 1
    Better use `math.fsum` instead of `sum` to minimize floating point inaccuracies. (And you don't have to use the `[`/`]` brackets in `sum`/`math.fsum` - a generator expression is enough.) – Timus Nov 10 '22 at 09:28
  • True - thanks, answer edited accordingly. – Vin Nov 10 '22 at 09:32
  • I am getting a TypeError: zip argument #2 must support iteration – sai Nov 10 '22 at 14:41
  • I've just copied and re-run the code I presented in my answer - it works for me, can you please check to make sure you've run it exactly as provided - no changes or typos? – Vin Nov 10 '22 at 16:22