Skip to main content
 首页 » 编程设计

python之如何找出列表中以元音开头的单词

2025年05月04日10shangdawei

words = ['apple', 'orange', 'pear', 'milk', 'otter', 'snake', 'iguana', 
         'tiger', 'eagle'] 
vowel=[] 
for vowel in words: 
    if vowel [0]=='a,e': 
        words.append(vowel) 
    print (words) 

我的代码不对,它会打印出原始列表中的所有单词。

请您参考如下方法:

words = ['apple', 'orange', 'pear', 'milk', 'otter', 'snake','iguana','tiger','eagle'] 
for word in words: 
    if word[0] in 'aeiou': 
        print(word) 

你也可以像这样使用列表理解

words_starting_with_vowel = [word for word in words if word[0] in 'aeiou']