繁体   English   中英

打印字典的相应元素

[英]printing the corresponding elements of dictionary

这是我在这里提出的问题的解决方案,该问题是分配一个带有两个组合的字符串并将它们分隔在列表中 前面的问题是一个示例,下面是实际的程序。

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']
            }   

def Usage() :
    print "Usage :python co.py phonemeFile"

def coart(phonemeFile):
    """ Function for generating parameter values looking 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 :
                    phonemeList.append((phoneme, param_values.get(phoneme)))

                else : 
                    phonemeList.extend([(phoneme, param_values.get(phoneme)) for phoneme in next_phoneme])

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

if __name__ == '__main__':
    if len(sys.argv) != 2 : 
        Usage() 

    else :
        phonemeFile = sys.argv[1]

        coart("syllabifiedPhonemes.txt")

输入文件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

我得到的输出如下:

New List
('s', None)
('aa', None)
("'", None)
('m', None)
('ih', None)
("'", None)
('k', None)
('l', None)
('eh', None)
('k', None)
("'", None)
('t', None)
('aa', None)
("'", None)
('n', None)
('ih', None)
('t', None)
("'", None)
('g', None)
('eh', None)
('l', None)
("'", None)
('v', None)
('ae', None)
("'", None)
('n', None)
('ih', None)
("'", None)
('k', None)
('aa', None)
("'", None)
('p', None)
('l', None)
('ao', None)
('ih', None)
('k', None)

现在的问题是,如何显示带有值的完整字典? 我可以看出,由于元音和辅音是分开的,所以打印方式不正确。 忽略元音和辅音标签,如何获得以下输出?

('s', [(-1, -1), (0.10000000000000001, 1.0), (-1, -1), (0.29999999999999999, 1.0), (0.0, 0.0), (-1, -1)]) 

PS对于撇号,无任何限制,因为它们不在字典中。

谢谢。

用以下内容替换添加音素的部分:

if next_phoneme is None :
    phonemeList.append((phoneme, param_values.get(phoneme)))
else : 
    phonemeList.extend([(phoneme, param_values.get(phoneme)) for phoneme in next_phoneme])

这将添加完整的元组,而不仅仅是名称。

代替

phonemeList.append(phoneme)

你需要:

phonemeList.append((phoneme, param_values[phoneme]))

您的打印对帐单也将得到简化,只需要是:

print phonemeList

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM