2011-01-19 83 views
0

我正在製作一個圖形來表示迷宮中的所有動作。事情是複製我重複動作時,使我的字典的輸出爲如下:通過創建一個新的圖形來改變圖形

{(1,2):[(2,2)],

(3,2):[(4- ,2),(3,3),(2,2)],

(3,3):[(3,2),(3,4)],

(5,2) :[(5,3),(4,2)],

(4,4):[(5,4),(3,4)],

(5,4):[ (5,3),(4,4)],

(2,2):[(3,2),(1,2)],

(4,2):[(5,2),(3,2)],

(3,4):[(4,4),(3,3)],

(5,3):[(5,2),(5,4)]}

任何想法如何我可以根據舊的一個新的字典,以及如何刪除重複的動作?

編輯:這本字典只是一個例子。

回答

0

辦法做到這將是:

# Here's your node collection 
toclean = {(1, 2): [(2, 2)], 
(3, 2): [(4, 2), (3, 3), (2, 2)], 
(3, 3): [(3, 2), (3, 4)], 
(5, 2): [(5, 3), (4, 2)], 
(4, 4): [(5, 4), (3, 4)], 
(5, 4): [(5, 3), (4, 4)], 
(2, 2): [(3, 2), (1, 2)], 
(4, 2): [(5, 2), (3, 2)], 
(3, 4): [(4, 4), (3, 3)], 
(5, 3): [(5, 2), (5, 4)]} 

# Here is a place to store nodes we've already seen 
seen = set() 

# Here is your new collection 
result = {} 

# Iterate over the original collection in sorted node order 
for key, values in sorted(toclean.items()): 
    # Mark node as seen 
    seen.add(key) 
    # Link it to any node that wasn't seen before 
    result[key] = [val for val in values if val not in seen] 

print result 

{(1, 2): [(2, 2)], 
(2, 2): [(3, 2)], 
(3, 2): [(4, 2), (3, 3)], 
(3, 3): [(3, 4)], 
(3, 4): [(4, 4)], 
(4, 2): [(5, 2)], 
(4, 4): [(5, 4)], 
(5, 2): [(5, 3)], 
(5, 3): [(5, 4)], 
(5, 4): []} 

但我想看看你是如何生成的圖形:濾波有更好。