从嵌套字典中访问值

时间:2011-09-18 15:12:38

标签: python

我正在尝试使用从全局嵌套字典访问的相应值来创建输入列表。这是代码,

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)

输入文件syllabifiedPhonemes.txt具有以下内容:

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

就我所见,if-else语句不正确。我收到以下错误:

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

我将if-else语句更改为以下内容并删除了错误。

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])

但是现在我把输出作为一个巨大的列表,我假设程序多次遍历dict并一次又一次地打印整个dict,而不是只显示给定输入的值。有人能指出我错在哪里吗?谢谢。

4 个答案:

答案 0 :(得分:1)

不应该在那里使用

param_values.get,因为您已经使用param_values[...]来读取值。

即。而不是:

param_values.get(param_values[group][phones])

使用:

param_values[group][phones]

如果您希望在密钥不存在时使用默认值,则可以使用get代替[...],而不是{{1}},但在这种情况下,这不是必需的。< / p>

答案 1 :(得分:1)

以下是您正在做的事情:

param_values.get(param_values['vowels']['aa'])

但问题在于

param_values['vowels']['aa'] 

返回一个列表,并且您尝试使用列表获取地图中的值,该列表不可用,您不能将其用作地图中的键。

我相信你想做的就是得到什么

param_values['vowels']['aa']

返回

在python控制台中尝试一下,你会发现错误。

检查出来:

What can be a dictionary key? 

Strings can be dictionary keys: 

mydict = {"dog":"Hund", "rhinoceros":"Nashorn"} 

Integers can be dictionary keys. The following dictionary (somewhat redundantly) 
maps prime numbers to their rank, e.g. 2 is the first prime number. 

prime_nums = {2:1, 3:2, 5:3, 7:4, 11:5} 

Floating point numbers can be dictionary keys as well: 
>>> mydict = {3.1415 : "pi", 2.71828 : "e" } 
>>> mydict[3.1415] 
'pi’ 

Lists cannot be dictionary keys
>>> mydict = {[1,2] : "wont_work"} 
Traceback (most recent call last): 
File "<stdin>", line 1, in <module> 
 TypeError: unhashable type: 'list' 

More info


好的,首先,尝试使用以下代码迭代dict

for k, v in dict.items():
    # k is the key
    # v is the value
    pass

它会使您的代码更清晰,也许它可以解决您的问题。

答案 2 :(得分:1)

尝试这样的事情。它更简单,更有效(我认为)

import sys

param_values = {
            '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)] ,   
            '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']
        }   


phonemeList = []
with open("syllabifiedPhonemes.txt", "r") as pFile :
    for line in pFile :
        l = line.split()
        for phoneme in l : 
            if phoneme != "'":
                next_phoneme = diphthong.get(phoneme)
                if next_phoneme is None :
                    # exploring the dict param_values extracting each nested dict
                    phonemeList.append((phoneme, param_values[phoneme]))
                else : 
                    phonemeList.extend([(phoneme, param_values[phoneme]) for phoneme in next_phoneme])

        print "New List"
        print '\n'.join(str(l) for l in phonemeList)

如果您需要辅音/元音信息,最好将其存储在其他地方。在这里存储它太复杂了代码。

注意:我将该功能用于测试目的。显然你应该把它放回你的代码中;)

答案 3 :(得分:1)

这是对您当前的新代码版本代码段的回复:

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])

如果我理解正确的目标,我认为你想要的东西可以写成:

next_phonemes = diphthong.get(phoneme, [phoneme])
for next_phoneme in next_phonemes:
    # exploring the dict param_values extracting each nested dict
    for group_val in param_values.itervalues(): # 'group_val' refers to values associated to vowels or consonants
        if next_phoneme in group_val:
            phonemeList.append((next_phoneme, group_val[next_phoneme])
            break # Once we found the next phoneme, no need to look further