代码之家  ›  专栏  ›  技术社区  ›  TheDoctor

将python字典转换为流程图

  •  1
  • TheDoctor  · 技术社区  · 11 年前

    我有一个程序,它将生成一个非常大的字典样式列表,看起来像这样:

    {"a":"b",
     "b":"c",
     "C":"d",
     "d":"b",
     "d":"e"}
    

    我想创建一个程序,使用类似pygame的东西来生成一个流程图,使用箭头将所有第一个术语连接到最后一个术语。这将忽略重复的连接,并在项目加倍时生成项目循环。

    如果处理了上面的列表,它会是这样的(请原谅手绘): enter image description here

    2 回复  |  直到 11 年前
        1
  •  2
  •   Nykakin    11 年前

    使用 Graph Tool :

    from graph_tool.all import *
    
    g = Graph()
    
    vals = [("a","b"), ("b","c"), ("c","d"), ("d","b"), ("c","e")]
    
    vertexes_names = g.new_vertex_property("string") 
    vertexes = {}
    for start, stop in vals:
        if start not in vertexes:
            vertexes[start] = g.add_vertex()
            vertexes_names[vertexes[start]] = start
        if stop not in vertexes:
            vertexes[stop] = g.add_vertex()
            vertexes_names[vertexes[stop]] = stop
        g.add_edge(vertexes[start], vertexes[stop])
    
    graph_tool.stats.remove_parallel_edges(g)
    graph_draw(g, vertex_text=vertexes_names, vertex_font_size=18, output="output.png")
    

    enter image description here

        2
  •  2
  •   John La Rooy    11 年前

    当我需要这样做的时候,我用 pydot graphviz