使用python在单词列表中显示元音
我怎样才能得到一个像上面那样的列表并拥有python呢使用python在单词列表中显示元音,python,list,loops,count,Python,List,Loops,Count,我怎样才能得到一个像上面那样的列表并拥有python呢 检查列表中的每三个元素,并计算该字符串中的元音数 打印字符串及其包含的元音数 如果字符串包含3个或更多元音,则退出循环 要从世界列表中提取每三个元素,请使用: wordlist = ['dog', 'cat', 'mouse', 'alpaca', 'penguin', 'snail'] 这意味着,从索引0开始,通过列表将索引增加三 现在,要获得元音的数量,您有很多选择: 使用列表理解,可以执行以下操作: wordlist[0::3]
要从
世界列表
中提取每三个元素,请使用:
wordlist = ['dog', 'cat', 'mouse', 'alpaca', 'penguin', 'snail']
这意味着,从索引0开始,通过列表将索引增加三
现在,要获得元音的数量,您有很多选择:
- 使用列表理解,可以执行以下操作:
wordlist[0::3]
- 使用正则表达式并替换:
vowels = 'aeiouy' vowels_only = [c for c in w.lower() if c in 'aeiou']
import re
re.sub(r'[aeiou]', '', w, flags=re.IGNORECASE)
wordlist = ['dog', 'cat', 'mouse', 'alpaca', 'penguin', 'snail']
for w in wordlist[0::3]:
vowels_only = [c for c in w.lower() if c in 'aeiou']
nb_vowels = len(vowels_only)
print("%s (%d)" % (w, nb_vowels)) # print the word and vowels count
if nb_vowels >= 3:
break