2011-03-26 26 views
4

傢伙我寫了一段代碼給出了輸出。現在,這是一個線索。現在我想以審美的方式展示。有人幫我在這裏。我的表示應該是這樣的http://en.wikipedia.org/wiki/File:Trie_example.svgPython Dicts:需要一個更好的輸出

但我想要的是如何將這個巨大的怪物輸出轉換爲整齊的輸出,如[(1,2),(1,3),(1,4),(3 ,4)] ????

defaultdict(<type 'int'>, {'A': 1, 'W': 12}) 
defaultdict(<type 'int'>, {'A': 2, 'X': 25}) 
defaultdict(<type 'int'>, {'A': 3}) 
defaultdict(<type 'int'>, {'A': 4}) 
defaultdict(<type 'int'>, {'S': 5}) 
defaultdict(<type 'int'>, {'S': 6}) 
defaultdict(<type 'int'>, {'S': 7}) 
defaultdict(<type 'int'>, {'D': 8}) 
defaultdict(<type 'int'>, {'D': 9}) 
defaultdict(<type 'int'>, {'D': 10}) 
defaultdict(<type 'int'>, {'D': 11}) 
defaultdict(<type 'int'>, {}) 
defaultdict(<type 'int'>, {'R': 16, 'E': 13, 'F': 19}) 
defaultdict(<type 'int'>, {'E': 14}) 
defaultdict(<type 'int'>, {'E': 15}) 
defaultdict(<type 'int'>, {}) 
defaultdict(<type 'int'>, {'R': 17}) 
defaultdict(<type 'int'>, {'T': 18}) 
defaultdict(<type 'int'>, {}) 
defaultdict(<type 'int'>, {'F': 20}) 
defaultdict(<type 'int'>, {'F': 21}) 
defaultdict(<type 'int'>, {'D': 22}) 
defaultdict(<type 'int'>, {'D': 23}) 
defaultdict(<type 'int'>, {'D': 24}) 
defaultdict(<type 'int'>, {}) 
defaultdict(<type 'int'>, {'C': 26}) 
defaultdict(<type 'int'>, {'C': 27}) 
defaultdict(<type 'int'>, {'V': 28}) 
defaultdict(<type 'int'>, {'S': 29}) 
defaultdict(<type 'int'>, {}) 
+0

我得到了表示部分。但我想要的是如何將這個巨大的怪物輸出轉換爲整齊的輸出,如[(1,2),(1,3),(1,4),(3,4)]? – 2011-03-26 10:09:26

回答

16

您應該試試Pydot!這個軟件包允許你創建一些圖表行:

import pydot 

edges = [(1,2), (1,3), (1,4), (3,4)] 
g = pydot.graph_from_edges(edges) 
g.write_jpeg('graph_from_edges_dot.jpg', prog='dot') 

The result

進行安裝:

pip install pydot 

(你也可以使用easy_installPyPM如果你喜歡)

pydot需要pyparsing才能加載DOT文件和graphviz來呈現圖形。

如果你想有一個PNG圖片(例如),您可以通過

g.write('graph_from_edges_dot.png', prog='dot', format='png') 

g.write_png('graph_from_edges_dot.png', prog='dot') 

完整的文檔替換線

g.write_jpeg('graph_from_edges_dot.jpg', prog='dot') 

可以在這裏找到: PyDot documentation

+0

Thanx Seyz那真是太棒了! – 2011-03-26 10:07:13

+2

不客氣。如果你有你想要的所有信息,你可以接受答案。 – 2011-03-26 10:12:19