Ad

zip automatically iterates to the length of the shortest sequence passed in.

Code
Diff
  • from math import sqrt, pow
    
    # calculate the distance between two points in 2d,3d ... nD
    def distanceND(pA, pB, nD = None):
        return sqrt(sum(pow(a-b,2) for a,b in zip(pA,pB)))
    
    def distance2D(pA, pB):
        return(distanceND(pA, pB))
        
    def distance3D(pA, pB):
        return(distanceND(pA, pB))
    
    
    • from math import sqrt, pow
    • # calculate the distance between two points in 2d,3d ... nD
    • def distanceND(pA, pB, nD = None):
    • dist = 0
    • # if a number of dimension was not specified, use the smalest number of dimensions
    • if nD is None:
    • nD = min(len(pA),len(pB))
    • for i in range(nD):
    • dist += pow(pA[i] - pB[i], 2);
    • return(sqrt(dist));
    • return sqrt(sum(pow(a-b,2) for a,b in zip(pA,pB)))
    • def distance2D(pA, pB):
    • return(distanceND(pA, pB))
    • def distance3D(pA, pB):
    • return(distanceND(pA, pB))