【问题标题】:Convert python dictionary to flowchart将python字典转换为流程图
【发布时间】:2013-09-28 02:30:09
【问题描述】:

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

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

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

如果上面的列表被处理,它看起来像这样(请原谅手绘):

【问题讨论】:

  • 在您的列表中,您有 d→e 边缘,而在您的草图中则有 c→e。还要注意大写字母。

标签: python list dictionary bigdata data-processing


【解决方案1】:

使用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")

【讨论】:

    【解决方案2】:

    当我需要这样做时,我使用了pydotgraphviz

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-19
      • 2016-10-16
      • 1970-01-01
      • 2017-07-21
      • 2021-09-24
      • 2018-12-09
      • 2021-03-28
      • 2011-01-29
      相关资源
      最近更新 更多