def last_phonemes(phoneme_list):
""" (list of str) -> list of str
Return the last vowel phoneme and subsequent consonant phoneme(s) in
phoneme_list.
>>> last_phonemes(['AE1', 'B', 'S', 'IH0', 'N', 'TH'])
['IH0', 'N', 'TH']
>>> last_phonemes(['IH0', 'N'])
['IH0', 'N']
>>> last_phonemes(['B', 'S'])
[]
"""
for i, phoneme in reversed(list(enumerate(phoneme_list))):
if phoneme[-1] in '012':
return phoneme_list[i:]
return []
编辑解释
您想按reversed顺序遍历音素列表。当您找到第一个包含“0”字符的项时,您需要对原始列表进行切片(您的代码中正好有切片部分)。您将需要索引来进行切片,因此在反转之前enumerate先查看音素列表。在
相关问题 更多 >
编程相关推荐