9

I have a dictionary object as such:

menu = {'dinner':{'chicken':'good','beef':'average','vegetarian':{'tofu':'good','salad':{'caeser':'bad','italian':'average'}},'pork':'bad'}}

I'm trying to create a graph (decision tree) using pydot with the 'menu' data this.

'Dinner' would be the top node and its values (chicken, beef, etc.) are below it. Referring to the link, the graph function takes two parameters; a source and a node.

It would look something like this:

Except 'king' would be 'dinner' and 'lord' would be 'chicken', 'beef', etc.

My question is: How do I access a key in a value? To create a tree from this data I feel like I need to create a loop which checks if there is a value for a specific key and plots it. I'm not sure how to call values for any dictionary object (if it's not necessarily called 'dinner' or have as many elements.).

Any suggestions on how to graph it?

avasal
  • 14,350
  • 4
  • 31
  • 47
ono
  • 2,984
  • 9
  • 43
  • 85

2 Answers2

20

Using a recursive function

You might want to consider using a recursive function (like the visit in my code below), so that you are able to process a general nested dictionary. In this function, you want to pass a parent parameter to keep track of who is your incoming node. Also note you use isinstance to check if the dictionary value of a key is a dictionary of its own, in that case you need to call your visit recursively.

import pydot

menu = {'dinner':
            {'chicken':'good',
             'beef':'average',
             'vegetarian':{
                   'tofu':'good',
                   'salad':{
                            'caeser':'bad',
                            'italian':'average'}
                   },
             'pork':'bad'}
        }

def draw(parent_name, child_name):
    edge = pydot.Edge(parent_name, child_name)
    graph.add_edge(edge)
    
def visit(node, parent=None):
    for k,v in node.iteritems():# If using python3, use node.items() instead of node.iteritems()
        if isinstance(v, dict):
            # We start with the root node whose parent is None
            # we don't want to graph the None node
            if parent:
                draw(parent, k)
            visit(v, k)
        else:
            draw(parent, k)
            # drawing the label using a distinct name
            draw(k, k+'_'+v)

graph = pydot.Dot(graph_type='graph')
visit(menu)
graph.write_png('example1_graph.png')

Resulting tree structure

enter image description here

tripleee
  • 175,061
  • 34
  • 275
  • 318
greeness
  • 15,956
  • 5
  • 50
  • 80
  • Yes, this is exactly what I'm working on. The only thing I want to change is to add another bubble for 'bad', 'average', ... under the corresponding node. This is great though. – ono Dec 03 '12 at 19:42
  • Also, where do I stick the graph.write_png('...')? – ono Dec 03 '12 at 19:50
  • I just add that to my code for completeness. Thanks for accepting the answer. – greeness Dec 03 '12 at 19:51
  • Does this work for any dictionary data set? I tried a different data set and it doesn't seem to work – ono Dec 03 '12 at 20:03
  • For instance, If you have list in the dict, you need to take care of that as well in isinstance. – greeness Dec 03 '12 at 20:21
  • Also I assume all keys and "simple" values are in type of string. Otherwise you need to name them correctly when adding edges to pydot. – greeness Dec 03 '12 at 21:14
  • @greeness A question, if my content are in Mandarin, where can I encode? Thank you! – Makiyo Oct 24 '17 at 09:30
  • Not sure I understand what you mean. But if you mean the name of each node is in Chinese, could you just replace the key/value with uncode strings for the dictionary? – greeness Oct 24 '17 at 18:27
  • 2
    graph.write_png('example1_graph.png'), this write_png function gives me below error. Can anyone help me out of the error? Error: "dot" with args ['-Tpng', 'C:\\Users\\MYPC~1\\AppData\\Local\\Temp\\tmptnelq9i2'] returned code: 1 stdout, stderr: b'' b'Format: "png" not recognized. No formats found.\r\nPerhaps "dot -c" needs to be run (with installer\'s privileges) to register the plugins?\r\n' – miltonbhowmick Jul 29 '20 at 04:45
  • 2
    Yes, I also meet the same error, `FileNotFoundError: [Errno 2] "dot" not found in path.` – rosefun Aug 06 '20 at 12:11
1

Your question isn't entirely clear to me, but the way of accessing a dictionary key's value in Python is simply:

dictionary[key]

That will return to you that key's value. If that key is not in the dictionary, it will return a KeyError, so if you are working with dictionaries and you're not sure if the key you're requesting will be in the dictionary, you have two options.

If-statement (preferred):

if key in dictionary:
    return dictionary[key]

Try-catch:

try:
    return dictionary[key]
except KeyError:
    pass

If you don't know the keys in your dictionary and you need to get them, you can simply call dictionary.keys() and it will return a list of all of the keys in the dictionary.

Getting a the value of a dictionary key will return an object that could even be another object. Thus, to find out the value of "tofu", for example, we'd do the following:

menu['dinner']['vegetarian']['tofu']
# returns value 'good'
jdotjdot
  • 16,134
  • 13
  • 66
  • 118
  • Let me rephrase: Notice that the value for 'dinner' has keys in it. How do I access values for those keys? Not sure if that's clear. How can I get the value for the key 'tofu' for instance? – ono Dec 03 '12 at 17:34