2Xchampion
2Xchampion

Reputation: 656

Updating the nested dictionary under defaultdict

When attempting to update my dictionary under a nested collection.defaultdict Python gave me an error

Saying

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "program.py", line 18, in train_ngrams
    if graphemes[i] not in mydict[phonemes[i]].keys():
AttributeError: 'str' object has no attribute 'keys'

My code:

import csv
from collections import defaultdict

def train_ngrams(train_file):
    mydict = defaultdict(dict)
    phonemes = []
    graphemes = []
    with open(train_file, 'r') as f:
        reader = csv.reader(f) 
        next(reader)
        for p, g in reader:
            phonemes += p.split()
            graphemes += g.split()
            for i in range(len(phonemes)):
                if phonemes[i] not in mydict.keys():
                    mydict.update({phonemes[i] : graphemes[i]})
                    if graphemes[i] not in mydict[phonemes[i]].keys():
                        mydict[phonemes[i]].update({phonemes[i] : (graphemes.count(graphemes[i]) for graphemes[i] in graphemes) - 1})
                    else:
                        mydict[phonemes[i]][graphemes[i]] += 1         

Well thing is I am trying to update the dictionary as I iterate through a csv file. And here I first want to check if it is in the defaultdict already. If not then I wish to create a key and value pair.

And the value here in the defaultdict will actually be used later to implement a nested normal dictionary which stores some frequency stuff.

Here is an example:

defaultdict(<class 'dict'>, {'T': {'t': 2}, 'UH': {'oo': 1}})

What is a concise way to handle this error? Note that defaultdict is necessary in this part.

EDIT:

train_ngrams("training-data-ex1.csv")

The desired output should be

defaultdict(<class 'dict'>, {'T': {'t': 2}, 'UH': {'oo': 1}})

EDIT:

The sample txt file

phonemes,graphemes
T UH T,t oo t

Upvotes: 0

Views: 843

Answers (1)

tobias_k
tobias_k

Reputation: 82899

If you want to pair up elements, you have to use zip, not a double-for-loop.

mydict = collections.defaultdict(lambda: collections.defaultdict(int))
with open("training-data-ex1.csv") as f:
    reader = csv.reader(f)
    next(reader) # skip header
    for phonemes, graphemes in reader:
        for p, g in zip(phonemes.split(), graphemes.split()):
            mydict[p][g] += 1

This uses a defaultdict of defaultdict of int, so the result looks a bit weird, but it's essentially just what you wanted: defaultdict(<function <lambda> at 0x7fd297740840>, {'T': defaultdict(<class 'int'>, {'t': 2}), 'UH': defaultdict(<class 'int'>, {'oo': 1})}), or, without all that defaultdict boilerplate, {'T': {'t': 2}, 'UH': {'oo': 1}}.

Upvotes: 1

Related Questions