【问题标题】:Changing a graph by creating a new one通过创建新图表来更改图表
【发布时间】:2011-01-19 14:38:59
【问题描述】:

我正在制作一个代表迷宫中所有移动的图表。事情是在复制我重复的动作时,所以我的字典的输出如下:

{(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 dictionary graph logic


    【解决方案1】:

    一种方法是:

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

    但我想看看你是如何生成图表的:在那里过滤更好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多