I am reading a string from a file in my system. The string is as follows:
A -> B [ label="5.000" ];
B -> A [ label="-5.000" ];
I want to populate a set node
and dictionary adj
. Here's my code:
node = set()
adj = {}
with open("tiny_stn.dot",'r+t') as demofile:
lines = demofile.readlines()
node = {line.split()[0] for line in lines if "->" in line}
adj = {line.split()[2]:line.split('"')[1] for line in lines if '->' in line}
I want the set to be node={'A','B'}
and the dictionary to be adj={'A':{'B': 5.000}, 'B':{'A': -5.000}}
Populating the set works fine. The challenge is to populate a dictionary within a dictionary. The last line of code gives me inner dictionary of desired dictionary i.e. 'B': 5.000
as the first element.
Any help is appreciated.