使用python 2从列表中计算两点之间的距离

时间:2019-03-20 09:23:55

标签: python-2.7

我有2个列表,每个列表包含几个3D点。我的工作是首先合并list1的2点和list2的3点。然后从获得的组合中计算两个任意点之间的所有距离。仅当所有距离都大于0.5时,才打印出符合条件的组合(所有距离> 0.5)。我在python 2上编写了代码,并得到了AttributeError。您能帮我与python 2斗争吗?

from itertools import combinations
from itertools import product
import math

if __name__ == "__main__":
    list1 = ["0.0000  0.0000  0.2500", "0.0000  0.0000  0.2500", "0.0000  0.3333  0.2500"]
    list2 = ["0.0000  0.5000  0.7500", "0.5000  0.0000  0.7500", "0.0000  0.3333  0.7500", "0.2500  0.5000  0.7500"]
    r = 2
    s = 3
    minimum = 3.000
    l=[]
    combi1 = combinations(list1, r)
    combi2 = combinations(list2, s)
    com = product(combi1, combi2)
    for item in com:
        for j in range(len(item)):
            for k in range (0, 2):
                l[k] = [float(num) for num in item[j].split('  ')]
        for u in range(0,r):
            for v in range(1,r):
                if u<v:
                    x=l[v][0] - l[u][0]
                    y=l[v][1] - l[u][1]
                    z=l[v][2] - l[u][2]
                    distance = math.sqrt(math.pow(x, 2) + math.pow(y,2) + math.pow(z,2))
                    if distance < minimum:
                        minimum = distance
        if minimum <0.50:
            print item[j]

0 个答案:

没有答案