gpt4 book ai didi

python - 将python字典转换为流程图

转载 作者:太空宇宙 更新时间:2023-11-04 10:43:36 25 4
gpt4 key购买 nike

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

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

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

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

最佳答案

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

关于python - 将python字典转换为流程图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19062576/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com