如何在没有numpy的情况下计算距离

How to calculate distance without numpy

 myList =  [[0, 0, 0], #0
       [6.888437030500963, 5.159088058806049, -1.5885683833831], #1
       [2.0667720363602307, 5.384582486178219, -3.4898856343748133], #2
       [7.742743817055683, 1.4508370077567676,-3.957946551327696],#3
       [9.410384606156306, 9.613094711663472, -3.864209434979891],#4
       [5.047141494150383, 14.72917879480795, -1.4968295014732576],#5
       [0.05726832139919402,22.924103914172754, 8.158880019279806],#6
       [6.261613041330982, 30.96742292296441,4.361831405666459], #7
       [10.858248006533554, 38.94418868232428, 8.041510043975286],#8
       [10.30110231558782, 30.958212843691598, 6.724946753050958],#9
       [12.518841784463852,39.21843390844956, 16.057074108466132]]#10

import math

def distance (myList):

    dist = math.sqrt ((xa-xb)**2 + (ya-yb)**2 + (za-zb)**2)
    return dist

print("Distance:",(distance(myList)))

如何在没有 NumPy 的情况下计算所有这些点的距离?我知道如何使用 2 但不能超过 2

import math

def distance (myList):
  distlist=[]
  for a in myList:
    for b in myList:
      dist = math.sqrt ((a[0]-b[0])**2 + (a[1]-b[1])**2 + (a[2]-b[2])**2)
      distlist.append(dist)
  return distlist

print("Distance:",(distance(myList)))

您必须将每个结果附加到您之前生成的列表中,否则您将只存储最后一个值

我们可以用等式求出欧氏距离: d = sqrt((px1 - px2)^2 + (py1 - py2)^2 + (pz1 - pz2)^2)

在 python 中实施:

import math

def dist(list):
  cummulativeDist = 0

  # iterate over sets of points
  for i in range(len(list) - 1):
    coordInit = list[i]
    coordFinal = list[i+1]

    # distance from one pt to the next
    dist = math.sqrt(((coordInit[0] - coordFinal[0]) ** 2) 
                    + ((coordInit[1] - coordFinal[2]) ** 2) 
                    + ((coordInit[2] - coordFinal[2]) ** 2)) 

    cummulativeDist += dist

  return cummulativeDist

print(f"Distance: {dist(myList)}")

这将采用 3 维距离,从一个点到另一个点,return 总行进距离。

from math import sqrt

def euclidean_distance(x, y):
    return sqrt(sum((px - py)**2  for px, py in zip(x,y)))
    
euclidean_distance(x,y)