【发布时间】:2014-02-27 23:11:29
【问题描述】:
我编写了这个程序来计算一组点的 3d 距离。
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)]
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
结果是一个距离列表:
test = dist_3d(points)
print test
[[4.217700795331081, 5.922339064664832, 3.554222840244929], [2.1374049685457694, 7.046453008421205], [8.107835716151763]]
我希望得到的结果如下:
[4.217700795331081, 5.922339064664832, 3.554222840244929, 2.1374049685457694, 7.046453008421205, 8.107835716151763]
P.S.:代码没有优化,因为函数返回后的“points”列表只有一个元素
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)]
test = dist_3d(points)
points
[(472764.07, 6191524.39, 16.0)]
【问题讨论】:
标签: python list optimization coding-style