2017-04-20 55 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。