PYTHON:访问字典中列表列表的元素

时间:2018-09-09 03:03:03

标签: python list dictionary for-loop

我必须仔细检查一个单词的每个发音,这些单词都这样存储:每个单词都在字典中(以下称为wordAndPron),并且该词典中的每个单词都有多个发音作为列表存储。

{ 'RECORD' : 

[ ['R', 'AH0', 'K', 'AO1', 'R', 'D'], 

['R', 'EH1', 'K', 'ER0', 'D'], 

['R', 'IH0', 'K', 'AO1', 'R', 'D']]

}

{ 'MORTGAGE' :

[ ['M', 'AO1', 'R', 'G', 'AH0', 'JH'],

['M', 'AO1', 'R', 'G', 'IH0', 'JH']]

}

如何访问每个列表的元素?那么每个音素(例如“ R”和“ AH0”)将处于什么位置?

我将使用for循环来比较音素(找到押韵的单词),但是我不确定该怎么做。

def primary_stress_position(wordAndPron):
        stress = ''
        stressedPosition = 0
        phoneme = 0
        for phoneme in wordAndPron.values():   # <<< Problem area
                if phoneme.endswith('1'):
                        stress = phoneme
                        stressedPosition = position
                position += 1
        return stress, stressedPosition

如果这是一个非常明显/简单的问题,我深表歉意,但是我只是从Java启动Python,并且只选了另外一个编程类!

这是课堂作业。我不是问如何编写整个程序,而只是问如何访问各个元素。

3 个答案:

答案 0 :(得分:2)

由于wordAndPron字典的唯一值是列表列表,因此可以使用嵌套的for循环来访问其中的音素。而且,由于每个单词有多个发音,因此,您应该使函数yield成为发现对象,以便调用者可以从列表中获取给定单词的所有发音,而不是只返回列表中第一个发音的重读音素。生成器:

def primary_stress_position(wordAndPron):
    for entry in wordAndPron.values()[0]:
        for pron in entry:
            stress = ''
            stressedPosition = None
            position = 0
            for phoneme in pron:
                if phoneme.endswith('1'):
                    stress = phoneme
                    stressedPosition = position
                    break
                position += 1
            yield stress, stressedPosition

答案 1 :(得分:0)

将所有单词放在一个字典中。访问值和部分值,如下所示。

>>> d = {
'RECORD' :
[ ['R', 'AH0', 'K', 'AO1', 'R', 'D'],
['R', 'EH1', 'K', 'ER0', 'D'],
['R', 'IH0', 'K', 'AO1', 'R', 'D']],
'MORTGAGE' :
[ ['M', 'AO1', 'R', 'G', 'AH0', 'JH'],
['M', 'AO1', 'R', 'G', 'IH0', 'JH']]
}
>>> d['RECORD']
[['R', 'AH0', 'K', 'AO1', 'R', 'D'], ['R', 'EH1', 'K', 'ER0', 'D'], ['R', 'IH0', 'K', 'AO1', 'R', 'D']]
>>> d['RECORD'][0]
['R', 'AH0', 'K', 'AO1', 'R', 'D']
>>> d['RECORD'][0][0]
'R'
>>> 

我不清楚您要在功能中做什么,所以我不能说如何修改它。

答案 2 :(得分:0)

要访问字典中列表列表的元素,您只需使用三个for循环,这很简单,此代码不言自明:

d = { 'RECORD' : 

[ ['R', 'AH0', 'K', 'AO1', 'R', 'D'], 

['R', 'EH1', 'K', 'ER0', 'D'], 

['R', 'IH0', 'K', 'AO1', 'R', 'D']]

, 
 'MORTGAGE' :

[ ['M', 'AO1', 'R', 'G', 'AH0', 'JH'],

['M', 'AO1', 'R', 'G', 'IH0', 'JH']]

}


for outter_list in d.values():
    print(f'the outter_list now is: {outter_list} \n')
    for inner_list in outter_list:
        print(f'the inner_list now is: {inner_list}')
        for element in inner_list:
            print(f'element: {element}  from current innerlist: {inner_list}')
        print('\n')
    print('--------------------------------------------')     

输出:

the outter_list now is: [['R', 'AH0', 'K', 'AO1', 'R', 'D'], ['R', 'EH1', 'K', 'ER0', 'D'], ['R', 'IH0', 'K', 'AO1', 'R', 'D']]

the inner_list now is: ['R', 'AH0', 'K', 'AO1', 'R', 'D']
element: R  from current innerlist: ['R', 'AH0', 'K', 'AO1', 'R', 'D']
element: AH0  from current innerlist: ['R', 'AH0', 'K', 'AO1', 'R', 'D']
element: K  from current innerlist: ['R', 'AH0', 'K', 'AO1', 'R', 'D']
element: AO1  from current innerlist: ['R', 'AH0', 'K', 'AO1', 'R', 'D']
element: R  from current innerlist: ['R', 'AH0', 'K', 'AO1', 'R', 'D']
element: D  from current innerlist: ['R', 'AH0', 'K', 'AO1', 'R', 'D']


the inner_list now is: ['R', 'EH1', 'K', 'ER0', 'D']
element: R  from current innerlist: ['R', 'EH1', 'K', 'ER0', 'D']
element: EH1  from current innerlist: ['R', 'EH1', 'K', 'ER0', 'D']
element: K  from current innerlist: ['R', 'EH1', 'K', 'ER0', 'D']
element: ER0  from current innerlist: ['R', 'EH1', 'K', 'ER0', 'D']
element: D  from current innerlist: ['R', 'EH1', 'K', 'ER0', 'D']


the inner_list now is: ['R', 'IH0', 'K', 'AO1', 'R', 'D']
element: R  from current innerlist: ['R', 'IH0', 'K', 'AO1', 'R', 'D']
element: IH0  from current innerlist: ['R', 'IH0', 'K', 'AO1', 'R', 'D']
element: K  from current innerlist: ['R', 'IH0', 'K', 'AO1', 'R', 'D']
element: AO1  from current innerlist: ['R', 'IH0', 'K', 'AO1', 'R', 'D']
element: R  from current innerlist: ['R', 'IH0', 'K', 'AO1', 'R', 'D']
element: D  from current innerlist: ['R', 'IH0', 'K', 'AO1', 'R', 'D']


--------------------------------------------
the outter_list now is: [['M', 'AO1', 'R', 'G', 'AH0', 'JH'], ['M', 'AO1', 'R', 'G', 'IH0', 'JH']]

the inner_list now is: ['M', 'AO1', 'R', 'G', 'AH0', 'JH']
element: M  from current innerlist: ['M', 'AO1', 'R', 'G', 'AH0', 'JH']
element: AO1  from current innerlist: ['M', 'AO1', 'R', 'G', 'AH0', 'JH']
element: R  from current innerlist: ['M', 'AO1', 'R', 'G', 'AH0', 'JH']
element: G  from current innerlist: ['M', 'AO1', 'R', 'G', 'AH0', 'JH']
element: AH0  from current innerlist: ['M', 'AO1', 'R', 'G', 'AH0', 'JH']
element: JH  from current innerlist: ['M', 'AO1', 'R', 'G', 'AH0', 'JH']


the inner_list now is: ['M', 'AO1', 'R', 'G', 'IH0', 'JH']
element: M  from current innerlist: ['M', 'AO1', 'R', 'G', 'IH0', 'JH']
element: AO1  from current innerlist: ['M', 'AO1', 'R', 'G', 'IH0', 'JH']
element: R  from current innerlist: ['M', 'AO1', 'R', 'G', 'IH0', 'JH']
element: G  from current innerlist: ['M', 'AO1', 'R', 'G', 'IH0', 'JH']
element: IH0  from current innerlist: ['M', 'AO1', 'R', 'G', 'IH0', 'JH']
element: JH  from current innerlist: ['M', 'AO1', 'R', 'G', 'IH0', 'JH']


--------------------------------------------