2017-06-05 48 views
-2

我有一个点的列表,我必须找到每对点之间的距离。所以,创建一个函数来找到距离在这里并不重要,但是要创建一个循环或函数来检查每对之间的距离是否已经计算出来。如何检查一对的每个组合?

列表下面的每个项目都表示对应点的ID。

l = [a, b, c, d, e, f] 

我想创建,检查每对之间的距离已被计算,如果没有的话,它计算一个函数的距离的函数表示: distance_method(A,B)

并存储该值在数据帧中为:

point1  point2   distance 
a   b    some_val 
a   c    some_val 

注:距离(A,b)=距离(b,A)

+0

请添加代码,你怎么尝试过了,什么你的代码的问题。在StackOverflow上,您不应该要求其他人为您编写代码。 – Nef10

回答

3

您会想要的itertools模块

>>> import itertools 
>>> lst = ['a', 'b', 'c', 'd'] 
# get pairs from this list, such that you will get things like 
# ('b', 'd') but not the reverse ('d', 'b') 
>>> list(itertools.combinations(lst, 2)) 
[('a', 'b'), ('a', 'c'), ('a', 'd'), ('b', 'c'), ('b', 'd'), ('c', 'd')] 

所以,我想你会想要做类似的信息(伪代码如下):

for pair in itertools.combinations(lst, 2): 
    if not in_dataframe(pair): 
     add_to_dataframe(pair) 
相关问题