accessing values from a nested dictionary
<p>I am trying to create a list of the input with their corresponding values accessing from the global nested dictionary. Here is the code,</p>

<pre><code>import sys

param_values = {
            'vowels':{
            'aa' : [(-1,-1),   (-1,-1),    (-1,-1),   (-1,-1),   (0.1,1.0),   (-1,-1)],     
            'ae' : [(-1,-1),   (-1,-1),    (0.1,0.8), (-1,-1),   (0.1,1.0),   (-1,-1)], 
            'ah' : [(-1,-1),   (-1,-1),    (-1,-1),   (-1,-1),   (0.1,1.0),   (-1,-1)], 
            'ao' : [(-1,-1),   (-1,-1),    (-1,-1),   (-1,-1),   (0.2,1.0),   (-1,-1)], 
            'eh' : [(-1,-1),   (-1,-1),    (-1,-1),   (-1,-1),   (0.1,1.0),   (-1,-1)], 
            'er' : [(-1,-1),   (-1,-1),    (-1,-1),   (-1,-1),   (0.15,0.7),  (-1,-1)], 
            'ey' : [(-1,-1),   (-1,-1),    (0.3,1.0), (-1,-1),   (0.1,0.5),   (-1,-1)],
            'ih' : [(-1,-1),   (-1,-1),    (-1,-1),   (-1,-1),   (0.1,0.8),   (-1,-1)], 
            'iy' : [(-1,-1),   (-1,-1),    (0.2,1.0), (-1,-1),   (0.1,0.8),   (-1,-1)], 
            'uh' : [(-1,-1),   (-1,-1),    (-1,-1),   (-1,-1),   (-1,-1),     (0.1,1.0)], 
            'uw' : [(-1,-1),   (-1,-1),    (-1,-1),   (-1,-1),   (-1,-1),     (0.1,1.0)],   
            'o'  : [(-1,-1),   (-1,-1),    (-1,-1),   (-1,-1),   (-1,-1),     (0.4,1.0)]    
            },
            'consonants':{
            'b'  : [(-1,-1),   (0.0,0.0),  (-1,-1),   (-1,-1),   (0.1,0.8),   (-1,-1)], 
            'ch' : [(-1,-1),   (-1,-1),    (-1,-1),   (-1,-1),   (0.1,0.4),   (-1,-1)], 
            'd'  : [(-1,-1),   (-1,-1),    (-1,-1),   (-1,-1),   (0.1,0.4),   (-1,-1)], 
            'dh' : [(-1,-1),   (-1,-1),    (-1,-1),   (-1,-1),   (0.1,0.35),  (-1,-1)], 
            'dx' : [(-1,-1),   (-1,-1),    (-1,-1),   (-1,-1),   (0.1,0.35),  (-1,-1)], 
            'f'  : [(0.3,1.0), (-1,-1),    (-1,-1),   (-1,-1),   (-1,-1),     (-1,-1)], 
            'g'  : [(-1,-1),   (-1,-1),    (-1,-1),   (-1,-1),   (0.1,0.8),   (-1,-1)], 
            'hh' : [(-1,-1),   (-1,-1),    (-1,-1),   (-1,-1),   (0.1,0.8),   (-1,-1)], 
            'jh' : [(-1,-1),   (-1,-1),    (-1,-1),   (-1,-1),   (0.1,0.8),   (-1,-1)], 
            'k'  : [(-1,-1),   (-1,-1),    (-1,-1),   (-1,-1),   (0.1,0.8),   (-1,-1)], 
            'l'  : [(-1,-1),   (-1,-1),    (-1,-1),   (-1,-1),   (0.1,0.4),   (-1,-1)], 
            'm'  : [(-1,-1),   (0.0,0.0),  (-1,-1),   (-1,-1),   (0.1,0.8),   (-1,-1)], 
            'n'  : [(-1,-1),   (0.1,1.0),  (-1,-1),   (0.3,1.0), (0.0,0.0),   (-1,-1)], 
            'ng' : [(-1,-1),   (0.1,1.0),  (-1,-1),   (-1,-1),   (0.0,0.0),   (-1,-1)], 
            'p'  : [(-1,-1),   (0.0,0.0),  (-1,-1),   (-1,-1),   (0.1,0.8),   (-1,-1)], 
            'r'  : [(-1,-1),   (-1,-1),    (-1,-1),   (-1,-1),   (0.1,0.4),   (-1,-1)], 
            's'  : [(-1,-1),   (0.1,1.0),  (-1,-1),   (0.3,1.0), (0.0,0.0),   (-1,-1)],
            'sh' : [(-1,-1),   (0.1,1.0),  (-1,-1),   (0.3,1.0), (0.0,0.0),   (-1,-1)], 
            't'  : [(-1,-1),   (-1,-1),    (-1,-1),   (-1,-1),   (0.1,0.4),   (-1,-1)], 
            'th' : [(-1,-1),   (-1,-1),    (-1,-1),   (-1,-1),   (0.1,0.4),   (-1,-1)], 
            'v'  : [(0.3,1.0), (-1,-1),    (-1,-1),   (-1,-1),   (-1,-1),     (-1,-1)], 
            'w'  : [(-1,-1),   (-1,-1),    (-1,-1),   (-1,-1),   (-1,-1),     (0.1,1.0)], 
            'y'  : [(-1,-1),   (-1,-1),    (-1,-1),   (-1,-1),   (0.1,0.6),   (-1,-1)],  
            'z'  : [(-1,-1),   (0.1,1.0),  (-1,-1),   (0.3,1.0), (0.0,0.0),   (-1,-1)], 
            'zh' : [(-1,-1),   (-1,-1),    (-1,-1),   (-1,-1),   (0.1,0.6),   (-1,-1)] 
             }                         
            }

diphthong = { 
         'aw' : ['ao' , 'uw'],
         'ay' : ['ao' , 'ih'],
         'ow' : ['o' , 'aa'],
         'oy' : ['o' , 'ih']
        }   

print "Usage :python co.py phonemeFile"

def coart(phonemeFile):
""" Function for generating parameter values from the global list """
phonemeList = []
with open("syllabifiedPhonemes.txt", "r") as pFile :
    for line in pFile :
        l = line.split()
        for phoneme in l : 
            next_phoneme = diphthong.get(phoneme)
            if next_phoneme is None :
                # exploring the dict param_values extracting each nested dict
                for group in param_values.keys() : # 'group' refers to vowels or consonants
                    # iterate over each nested dict 
                    for phones in param_values[group].keys() : # 'phones' refers to the phonemes present inside each group 
                        phonemeList.append((phones, param_values.get(param_values[group][phones])))
            else : 
                for group in param_values.keys() : # 'group' refers to vowels or consonants
                    for phones in param_values[group].keys() : # 'phones' refers to the phonemes present inside each group 
                        phonemeList.extend([(phones, param_values.get(param_values[group][phones])) for phoneme in next_phoneme])

        print "New List"
        print '\n'.join(str(l) for l in phonemeList)
</code></pre>

<p>The input file syllabifiedPhonemes.txt has the following contents:</p>

<pre><code>s aa ' m ih ' k l eh k ' t aa ' n ih t ' g eh l ' v ae ' n ih ' k aa ' p l ay k
</code></pre>

<p>The if-else statement is not right as far as I can see. I got the following error:</p>

<pre><code>Traceback (most recent call last):
  File "co.py", line 189, in &lt;module&gt;
coart("syllabifiedPhonemes.txt")
  File "co.py", line 160, in coart
phonemeList.append((phones, param_values.get(param_values[group][phones])))
TypeError: unhashable type: 'list'
</code></pre>

<p>I changed the if-else statement to the following and got rid of the error.</p>

<pre><code>if next_phoneme is None :
    # exploring the dict param_values extracting each nested dict
    for group in param_values.keys() : # 'group' refers to vowels or consonants
        # iterate over each nested dict 
        for phones in param_values[group].keys() : # 'phones' refers to the phonemes present inside each group 
            phonemeList.append((phones, param_values[group][phones]))
else : 
    for group in param_values.keys() : # 'group' refers to vowels or consonants
        for phones in param_values[group].keys() : # 'phones' refers to the phonemes present inside each group 
            phonemeList.extend([(phones, param_values[group][phones]) for phoneme in next_phoneme])
</code></pre>

<p>But now I got the output as a huge list and I presume the program iterates through the dict many times and prints the whole dict again and again instead of just displaying the values of the given input. Could someone point out where I am wrong? Thank you.    </p>
