2013-09-28 24 views
1

我有一個程序,將產生一個非常大的字典樣式列表,將是這個樣子:轉換Python字典的流程圖

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

我想創建使用類似pygame的,生成的程序使用箭頭將所有第一項與最後一項連接起來的流程圖。這會忽略重複的連接,並且如果它們自己翻倍,則會生成項目循環。

如果被處理上面的列表中,它看起來像這樣(原諒的手繪圖): enter image description here

+0

在你的清單,你有d→EE dge和你的草圖c→e來代替。也要小心大寫字母。 – Nykakin

回答

2

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