Im making a graph that represents all the moves in a maze. Thing is when copying i repeated movements, so the output of my dictionary is as follow:
{(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)]}
Any idea on how i can make a n开发者_运维问答ew dictionary based on old one and how to remove the repeated movements?
Edit: This dictionary is just an example.
A way to do it would be:
# 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): []}
But I'd like to see how you generate the graph: filtering there is better.
精彩评论