2015-11-02 22 views
-1

我在列表中有一些對象,這裏是一個對象類:乾淨名單,並分成對

class MyData: 
    def __init__(self, name, x1, y1, x2, y2): 
     self.name = name 
     self.x1 = x1 
     self.y1 = y1 
     self.x2 = x2 
     self.y2 = y2 

列表中的某些對象有一個對通過obj.y2但有一個寬容t = 10。所以obj.y2 = 498obj.y2 = 502是成對的,因爲這些數字之間的差異小於10.某些對象沒有一對。所以我需要乾淨的列表只有對作爲輸出。

[(obj,obj), (obj,obj),...]我想知道使用itertools,但不能構建邏輯。任何幫助?

回答

0

itertools.combinations怎麼樣?

組合( 'ABCD',2) - > AB AC AD BC BD光盤

你的情況類似

for x in itertools.combinations(yourlist, 2): 
    // Pseudocode: your compare logic, something like if absolute(y2 - y1) < 10: 
    // then add to a different pair_only_list