2011-09-18 142 views
1

我想創建一個輸入列表,其中有相應的值從全局嵌套字典訪問。下面是代碼,訪問來自嵌套字典的值

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

但現在我得到的輸出作爲一個巨大的名單,我相信通過字典多次程序重複一次又一次打印整個字典,而不是隻顯示給定的輸入值。有人能指出我錯在哪裏嗎?謝謝。

+1

爲什麼你需要在這裏使用嵌套字典。用元音和輔音去除圖層可以簡化這個顯着的問題 –

+0

@james我必須使用嵌套字典,因爲在輸入音素列表後,他們的值主要是比較對方的值(範圍)後我必須做更多的事情。而他們對於音位是輔音或元音有不同的條件/規則。 – zingy

+0

,但通過將它們放入列表中,您將丟失該信息,將其放入單獨的數據結構並稍後檢索它會簡單得多。看到我的回答下面 –

回答

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) 

如果需要輔音/元音信息,最好是存放在別處。在這裏存儲代碼太複雜了。

注意:我拿出了用於測試目的的功能。很顯然,你應該把這個早在你的代碼;)

+0

我從相同的代碼開始,然後當我不得不將它們分離爲元音和輔音時,我爲該詞典添加了一個新圖層。我明白這簡單得多。就像你說的分開儲存元音和輔音將是一個更好的主意。這可以在文件中完成,並在進一步更新之前訪問該文件以查看音素是否是元音或輔音? – zingy

+0

爲什麼要使用文件?只需添加另一個字典,其中的鍵是音素,值是元音或輔音 –

+0

因爲我已經有一個文件,其中有相同的音素分成元音,輔音,音素等。該文件被我的其他程序用於音節化輸入,這就是我如何得到輸入文件中的撇號,你可以在我的問題中看到。所以我可以再次在這個程序中再次打開同一個文件。 – zingy

1

param_values.get不應該在那裏使用,因爲您已經使用param_values[...]來讀取值。即

代替:

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

用途:

param_values[group][phones] 

可以使用get代替(未除)[...],如果你想在鍵沒有默認值存在,但在這種情況下,這是沒有必要的。

+0

我忘了添加輸入在我的問題,所以只是編輯它。做你的建議我擺脫了錯誤,但我沒有得到所需的輸出。輸出就像一個巨大的清單。我認爲該程序一遍又一遍地重複列表,然後顯示整個字典。 – zingy

1

這裏是你在做什麼:

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

但問題是,

param_values['vowels']['aa'] 

返回一個列表,且您嘗試使用列表,以獲得在地圖的值不可用,你不能在地圖上用它作爲鍵。

我相信你想做什麼就是得到什麼

param_values['vowels']['aa'] 

回報

嘗試,在蟒蛇控制檯,你會找出錯誤。

檢查了這一點:

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


好吧,首先,嘗試通過字典使用下面的代碼

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

它會使你的代碼更清潔,也許它會解決你的問題。

+0

謝謝,是的,我通過這樣做擺脫了錯誤。現在我有一個不同的問題。我將編輯該問題。 – zingy

+0

我會試試這個。這裏的k是指嵌套字典嗎? – zingy

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 
+0

請注意,如jamesj指出的那樣(但我不能在他的回答中發表評論)使用'拼合'字典會更有效率。你可以這樣做:'flat_params = params_values ['vowels']。copy(); flat_params.update(params_values [ '輔音'])'。然後你可以用'phonemeList + = [(next_phoneme,flat_params [next_phoneme])代替next_phoneme中next_phoneme]' – tiho

+0

'的代碼來創建列表,我不需要知道哪個是元音,哪個是元音輔音。它只需要從全球列表中獲取相應的值。我需要確定哪一組音素在代碼中更進一步。所以我會把這個詞典改回原來的樣子,並使用我已經有的音素分成元音和輔音的文件。 – zingy