2011-01-19 11 views
0

迷路内のすべての動きを表すグラフを作っています。 iは動きを繰り返しコピーするときのものであるので、私の辞書の出力は次の通りである:新しいものを作成してグラフを変更する

{(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): []} 

しかし、私は、あなたがグラフを生成する方法を見てみたい:よりよいがありフィルタリング。

関連する問題