1

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?

1
  • 6
    What you show as the desired dictionary is not, in fact, valid python. Commented Jun 27, 2018 at 12:36

5 Answers 5

4

You were on the right track, but your defaultdict is a mapping from a string to list, and not dictionnary. Try this :

g = defaultdict(list) for i in new_grammar: g[i[0]].extend(i[1:]) 
Sign up to request clarification or add additional context in comments.

1 Comment

Or possibly use list.append instead of list.extend. It's not really clear what output the asker is looking for, since the example in the question isn't valid python.
2

This is one way using collections.defaultdict.

Note the result is a dictionary with a list of values assigned to each key. This is not how you have defined your desired output, which is not valid Python.

L = [['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'"]] from collections import defaultdict d = defaultdict(list) for k, *v in L: d[k].extend(v) print(d) defaultdict(list, {'Det': ["'the'"], 'N': ["'pirate'", "'sailor'", "'telescope'"], 'NP': ['Det', 'N', 'NP', 'PP'], 'P': ["'with'"], 'PP': ['P', 'NP'], 'S': ['NP', 'VP'], 'V': ["'sees'"], 'VP': ['V', 'NP', 'VP', 'PP']}) 

Comments

0

Why not just this? or is this what you want?:

l = [['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'"]] print(dict(zip([i[0] for i in l],[i[1:] for i in l]))) 

Output:

{'S': ['NP', 'VP'], 'NP': ['NP', 'PP'], 'VP': ['VP', 'PP'], 'PP': ['P', 'NP'], 'Det': ["'the'"], 'N': ["'telescope'"], 'V': ["'sees'"], 'P': ["'with'"]} 

Comments

0

How about this: add to the dictionary if element is not present. Append to dictionary if element is present.

l = [['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'"]] grammar = {} for rule in l: if(grammar.get(rule[0],None) == None): grammar[rule[0]] = rule[1:] else: for elem in rule[1:]: grammar[rule[0]].append(elem) print grammar 

Comments

0

Can also use itertools.groupby():

from itertools import groupby from itertools import chain from operator import itemgetter from pprint import pprint L = [['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'"]] result = {k: list(chain(*(x[1:] for x in g))) for k, g in groupby(sorted(L, key=itemgetter(0)), key=itemgetter(0))} pprint(result) 

Which Outputs:

{'Det': ["'the'"], 'N': ["'pirate'", "'sailor'", "'telescope'"], 'NP': ['Det', 'N', 'NP', 'PP'], 'P': ["'with'"], 'PP': ['P', 'NP'], 'S': ['NP', 'VP'], 'V': ["'sees'"], 'VP': ['V', 'NP', 'VP', 'PP']} 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.