0

I'm new to python, and I am trying to convert a dictionary into a graph object in Networkx. The dictionary:

dict = {'key1': ['value1', 'value2', 'value3'], 'key2': ['value1', 'value2']}

I want the keys to be an ego node, and values to be their alter nodes (with out-degree going from an ego to an alter). Is this possible to achieve in Python?.

Zlo
  • 1,150
  • 2
  • 18
  • 38

1 Answers1

-1

You can iterate over keys just by using the dictionary as an iterable:

for key in d:
    #...

and, as you probably already know, you access values in the dictionary using the key as "index":

d[key]

Iterating over lists (which your dictionary values are) is similar to iterating over the keys of the dictionary.

Combining these pieces of knowledge with the excellent networkx example on creating graphs should be straightforward, so you should try it, and ask a new question if you have any specific problem

loopbackbee
  • 21,962
  • 10
  • 62
  • 97
  • 1
    Thanks, also from_dict_of_lists Networkx function works well. I should have read the manual first. – Zlo May 19 '14 at 18:42