Reputation: 133
I have to go through each pronunciation of a word, which I've stored as such: each word is in a dictionary (below called wordAndPron), and each word in that dictionary has several pronunciations stored as lists.
{ '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']]
}
How do I access the elements of each list? So what position would each of the phonemes, like 'R' and 'AH0', be?
I'm going to be using a for loop to go through and compare phonemes to one another (finding rhyming words), but I'm unsure how to do so.
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
I apologize if this is a very obvious/simple question, but I'm just starting Python from Java and have only taken one other programming class!
This is classwork. I'm not asking how to write the entire program, only how to access the individual elements.
Upvotes: 3
Views: 1557
Reputation: 449
to access elements of a list of lists in a dictionary you just need to use three for loops, it's fairly simple, this code is self-explanatory:
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('--------------------------------------------')
Output:
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']
--------------------------------------------
Upvotes: 0
Reputation: 106455
Since the one and only value of the wordAndPron
dict is a list of lists, you can use a nested for
loop to access the phonemes within. And since you have several pronunciations per word, instead of returning just the stressed phoneme of the first pronunciation in the list, you should make the function yield
the finding so that the caller can obtain all the pronunciations of a given word from the generator:
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
Upvotes: 3
Reputation: 19144
Put all the words in one dictionary. Access values and parts of values as follows.
>>> 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'
>>>
It is not clear to me what you are trying to do in your function, so I cannot say how to revise it.
Upvotes: 0