gpt4 book ai didi

python - 从嵌套字典访问值

转载 作者:太空宇宙 更新时间:2023-11-04 10:58:50 25 4
gpt4 key购买 nike

我正在尝试创建一个输入列表,其中包含从全局嵌套字典访问的相应值。这是代码,

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

但现在我得到的输出是一个巨大的列表,我假设程序多次遍历字典并一次又一次地打印整个字典,而不是仅仅显示给定输入的值。有人可以指出我错在哪里吗?谢谢。

最佳答案

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

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)

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

注意:出于测试目的,我取出了该函数。显然你应该把它放回你的代码中;)

关于python - 从嵌套字典访问值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7462361/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com