问题描述
我有两个列表char_list和word_list,我需要映射这两个列表,并输出带有相应字符和分数的单词。 例: {'TUW':[{'T':[100.0],'UW':[100.0]}
我目前正在使用defaultdict来克服单词重复的问题
g_scores={'T': [58.53405353527961,78.25044195239214,68.06368641487768,43.13945776610137,100.0],'UW': [100.0],'AH': [100.0,100.0,'N': [100.0,'D': [100.0,65.78726377333408],'ER': [18.498848245281135,66.23670087349258],'S': [100.0,'AE': [83.39396047440374,78.73869843039938],'DH': [67.77794712265037,'IH': [100.0,'EH': [99.39733772255123],'P': [0.0,'L': [100.0],'IY': [100.0,'Z': [70.4984347755229],'AA': [100.0],'AY': [100.0],'V': [96.92156901210153],'B': [100.0],'M': [100.0],'JH': [100.0]}
word_list=['TUW','AHNDERSTAEND','DHIHS','STEHP','PLIYZ','TAEP','AON','DHIY','ERAYVD','BAHTAHN','IHN','IHMIHJH']
char_list=['T','UW','AH','N','D','ER','S','T','AE','DH','IH','EH','P','L','IY','Z','AA','AY','V','B','M','JH']
occured_phones=[]
sep_scores=defaultdict(list)
for word_p in word_list:
scores = defaultdict(list)
word_score=0
sum_score=0
p_s=""
if str(word_p) != "NULL":
for j in p_hones:
if len(word_p) == len(p_s):
#removing the chars from char_list
for i in scores.keys():
for j in range(len(scores[i])):
char_list.remove(i)
break
else:
if j in word_p:
p_s=p_s+j
if j in occured_phones:
scores[j].append(g_scores.get(str(j))[-1])
del g_scores.get(str(j))[-1]
else:
scores[j].append(g_scores.get(str(j))[-1])
sum_score=sum_score+g_scores.get(str(j))[-1]
#getting number of elemets in scores dict (counting length of lists and summing)
sns=[]
for k,vals in scores.items():
for i in range(len(vals)):
sns.append(k)
if len(''.join(sns)) == len(word_p):
scores['word_score'] = (sum_score // (len(scores.keys())))
sep_scores[str(word_p)].append(dict(scores))
else:
score={'word_score':0}
sep_scores[str(word_p)].append(dict(score))
final_dict['phone_scores'] = dict(sep_scores)
print(final_dict)
当前输出
'phone_scores': {'TUW': [{'T': [100.0],'word_score': 100.0}],'AHNDERSTAEND': [{'AH': [100.0],'D': [65.78726377333408,'ER': [66.23670087349258],'S': [100.0],'T': [100.0],'AE': [78.73869843039938],'N':[100.0],'D':[ 'word_score': 110.0}],'DHIHS': [{'DH': [100.0],'IH': [100.0],'STEHP': [{'T': [100.0],'P': [100.0,'word_score': 133.0}],'PLIYZ': [{'L': [100.0],'IY': [100.0],'P': [100.0],'word_score': 92.0}],'TAEP': [{'T': [100.0,'word_score': 139.0}],'AON': [{'N': [100.0,'word_score': 300.0}],'DHIY': [{'DH': [100.0],'ERAYVD': [{'ER': [66.23670087349258],'D': [65.78726377333408],'word_score': 82.0}],'BAHTAHN': [{'word_score': 0}],'IHN': [{'word_score': 0}],'IHMIHJH': [{'IH': [100.0,'word_score': 200.0}]}}
通知:
单词“ STEHP”中没有“ S”,单词“ TAEP”中没有“ P”,“ BAHTAHN”和“ IHN”为空, 在AHNDERSTAEND的值“ N”,“ D”中,一个键的当前输出显示在列表中,因为它们重复了,所以将它们的值赋予单个键,是否可以分别打印其分数? 单词分数的问题应该限制在0-100之间。
预期产量
'phone_scores': {'TUW': [{'T': [100.0],'N': [100.0],'N' : [100.0],:[100.0],'word_score': 110.0}],'STEHP': [{'S':[100.0],'word_score': 90...}],'BAHTAHN': [{'B':[100.0],'AH':[90.0],'T':[80.0],'IHN': [{'IH':[90.0],'word_score': 95}],'IHMIHJH': [{'IH': [100.0],'IH':[100.0],'JH':[100.0]'word_score': 100.0}]}}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)