I am trying to work on this problem which basically takes a dictionary, table_one
, with lists as it's values and i am trying to convert it into another dictionary, table_two
, where the keys are values from table_one
, however the values must match with the keys from table_one
Basically i want to convert this:
table_one = {'e0': ['v0', 'v1', 'v5'], 'e1': ['v1', 'v3', 'v4', 'v5'], 'e2':['v0', 'v2', 'v5']}
into this
table_two = {'v0' : ['e0', 'e2'], 'v1' : ['e0', 'e2'], 'v3':['e1'], 'v4' : ['e1'], 'v5' : ['e0', 'e1', 'e2']
So far i have come up with this code:
table_one = {'e0': ['v0', 'v1', 'v5'], 'e1': ['v1', 'v3', 'v4', 'v5'],
'e2':['v0', 'v2', 'v5']}
table_two = {}
for lists in table_one.values():
for elements in lists: #iterates through values in each list
if elements in table_one: #checks whether element belongs to key
table_two[elements].append(table_one.keys())
print "Elements = ", table_two.items()'
I run into a logical error, with absolutely no output. Any suggestions?