def count_vowel_phonemes(phonemes):
""" (list of list of str) -> int
Return the number of vowel phonemes in phonemes.
>>> phonemes = [['N', 'OW1'], ['Y', 'EH1', 'S']]
>>> count_vowel_phonemes(phonemes)
2
"""
number_of_vowel_phonemes = 0
for phoneme in phonemes:
for item in phoneme:
if 0 or 1 or 2 in item:
number_of_vowel_phonemes = number_of_vowel_phonemes + 1
return number_of_vowel_phonemes
说明: 元音音素是最后一个字符是0、1或2的音素。例如,单词BEFORE(bih0fao1r)包含两个元音音素,而单词GAP(gap1p)则有一个。在
参数表示音素列表。函数的作用是返回在音素列表中找到的元音音素的数量。 基本上这个问题要求我计算这个列表中的位数,但是我的代码总是返回0,这很奇怪。我的代码有问题吗?提前谢谢!!在
您的代码实际上会为您的测试输入返回}而不是实际检查{}是否在{}中,并且还将}进行比较。在
5
,因为or 1
的计算结果总是True
,您检查的是{ints
与{可以使用
^{pr2}$sum
将代码简化为生成器表达式,并使用str.endswith
查找以0、1或2结尾的子元素:哪些输出:
使用
or
进行测试的正确方法是:相当于
any
行。在相关问题 更多 >
编程相关推荐