Python Dicts: Need Better Results

Guys, I wrote a piece of code that gives the result. Now this is a trick. Now I want to prove myself in an aesthetic manner. Someone will help me here. My view should be like this: http://en.wikipedia.org/wiki/File:Trie_example.svg

But I want how to convert this huge monster output into a neat output like this [(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'>, {}) 
+4
source share
1 answer

You should try Pydot ! This package allows you to create several graphs:

 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

To install it:

 pip install pydot 

(you can also use easy_install or PyPM if you want)

pydot needs pyparsing to load DOT and graphviz files to display graphs.

If you want to have a png image (for example), you can replace the line

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

by

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

or

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

Full documentation is available here: PyDot Documentation

+16
source

Source: https://habr.com/ru/post/1345321/


All Articles