I need to convert a list of lists like so:
[['S', 'NP', 'VP'], ['NP', 'Det', 'N'], ['NP', 'NP', 'PP'], ['VP', 'V', 'NP'], ['VP', 'VP', 'PP'], ['PP', 'P', 'NP'], ['Det', "'the'"], ['N', "'pirate'"], ['N', "'sailor'"], ['N', "'telescope'"], ['V', "'sees'"], ['P', "'with'"]] to a dictionary such that it looks like this:
{'S':['NP', 'VP'], 'NP': ['Det', 'N'], ['NP', 'PP'], 'VP': ['V', 'NP'], ['VP', 'PP'], 'PP': ['P', 'NP'], 'Det': ["'the'"], 'N': ["'pirate'"], ["'sailor'"], ["'telescope'"], 'V': ["'sees'"], 'P': ["'with'"]} I have tried using this method using from collections import default dict:
g = defaultdict(dict) for i, j, k in new_grammar: g[i][j] = k But this does not work because there are lists in the list of lists with only two elements.
I have also tried:
grammar = {} for rule in new_grammar: grammar[rule[0]] = rule[1:] However, this only gives each key one value.
Is there any way to do this?