Minimum, mean and maximum distance between points 3-D in Python
Question: I have a list of x,y,z points. Using the formula to find the distance between
two points in 3-D

    
    
    import math
    import numpy as np
    
    point0 = x0, y0, z0
    point1 = x1, y1, z1
    
    dist = math.sqrt((x0-x1)**2+(y0-y1)**2+(z0-z1)**2)
    
    def dist3d((x0, y0, z0), (x1, y1, z1)):
        return math.sqrt((x0-x1)**2+(y0-y1)**2+(z0-z1)**2)
    

i wish to write a optimized loop and store the distance

points = [(472765.09, 6191522.78, 13.0), (472764.82, 6191524.09, 9.0),
(472763.8, 6191525.68, 8.0), (472764.07, 6191524.39, 16.0)]

    
    
    dist01 = dist3d(test[0],test[1])
    dist02 = dist3d(test[0],test[2])
    dist03 = dist3d(test[0],test[2])
    dist04 = dist3d(test[0],test[2])
    
    dist12 = dist3d(test[1],test[2])
    dist13 = dist3d(test[1],test[3])
    
    dist23 = dist3d(test[2],test[3])
    
    3d_l=[(dist01),(dist02),(dist03),(dist04),(dist12),(dist13),(dist23)]
    
    3d_max =max(3d_l)
    3d_min = min(3d_l)
    3d_mean =  np.average(3d_l)
    

I wrote the following function (it's not optimized)

    
    
    def dist3d((x0, y0, z0), (x1, y1, z1)):
        return math.sqrt((x0-x1)**2+(y0-y1)**2+(z0-z1)**2)
    
    def dist_3d(obs):
        dist_list = list()
        while len(obs) != 1:
            obs_g = [(obs[0], x) for x in obs[1:]]
            dist_list.append([dist3d(obs_g[i][0], obs_g[i][1]) for i in xrange(len(obs_g))])
            obs.pop(0)
        return dist_list
    
    points = [(472765.09, 6191522.78, 13.0), (472764.82, 6191524.09, 9.0), (472763.8, 6191525.68, 8.0), (472764.07, 6191524.39, 16.0)]
    print dist_3d(points)
    [[4.217700795331081, 5.922339064664832, 3.554222840244929], [2.1374049685457694, 7.046453008421205], [8.107835716151763]]
    


Answer: If you don't mind using scipy, this is fairly trivial:

    
    
    import numpy as np
    import scipy.spatial.distance as distance 
    
    points = np.array([(472765.09, 6191522.78, 13.0), (472764.82, 6191524.09, 9.0), (472763.8, 6191525.68, 8.0), (472764.07, 6191524.39, 16.0)])
    
    dist = distance.pdist(points)
    print dist.max()
    print dist.min()
    print np.median(dist)
    print np.average(dist)
    

