Python 通过创建新图形来更改图形
我正在做一个图表,它代表了迷宫中的所有动作。事情是在复制我重复的动作时,所以我的字典的输出如下: {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]} 你知道我如何在旧字典的基础上编一本新字典,以及如何消除重复的动作吗Python 通过创建新图形来更改图形,python,dictionary,graph,logic,Python,Dictionary,Graph,Logic,我正在做一个图表,它代表了迷宫中的所有动作。事情是在复制我重复的动作时,所以我的字典的输出如下: {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]} 你知道我如何在旧字典的基础上编一本新字典,以及如何消除重复的动作吗 编辑:这本词典只是一个例子。一种方法是: # H
编辑:这本词典只是一个例子。一种方法是:
# 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): []}
但我想看看如何生成图形:过滤效果更好