2017-04-20 52 views
0

您好,我有以下問題。我有一個元組列表,其中每個元組都由3D座標組成。每個座標都在另一個時間步。他們真的很接近。是否有可能對列表進行製作/過濾,以便只有距離爲1的座標。因此,例如,我有Python:過濾器座標列表

list1=[(x1,y1,z1),(x2,y2,z2),(x3,y3,z3),(x4,y4,z4),(x5,y5,z5),(x6,y6,z6) 

和過濾器會產生

list2=[(x1,y1,z1),(x3,y3,z3),(x4,y4,z4),(x6,y6,z6)], 

因爲(x1,y1,z1),(x2,y2,z2)(x4,y4,z4),(x5,y5,z5)過於接近。

+0

您是否特指與列表中的相鄰元組相對,而不是列表中距離列表1中任何位置的任何對? – roganjosh

+1

同樣,你會如何定義「親密度」? – Divakar

+0

你可能想讀這個:[最近對算法](https://en.wikipedia.org/wiki/Closest_pair_of_points_problem) –

回答

1

由於矢量集的大小很小(< 100),所以可以使用一個簡單的解決方案。下面的代碼選擇集合中的代表,只要他們不接近已經選擇的現有代表。這段代碼在性能方面是天真的,它對元組的順序很敏感。根據問題的意見,它可能適合的問題:

import numpy as np 
from scipy.spatial.distance import euclidean 

def predicate(representatives, vector): 
    return all(euclidean(representative, vector) >= 1 
       for representative in representatives) 



def main(): 
    vectors = [tuple(l) for l in np.random.random_integers(0, 5, (100, 3))] 

    representatives = set() 
    for vector in vectors: 
     if predicate(representatives, vector): 
      representatives.add(vector) 

在我的機器(i5 6GB)需要約100ms。