vowels = 'aeiouy'
vowels_only = [c for c in w.lower() if c in 'aeiou']
使用regex并替换:
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
要从
worldlist
中获取每3个元素,请使用:这意味着,从索引0开始,然后遍历将索引递增3的列表。你知道吗
现在,要得到元音的数量,你有很多选择:
使用列表理解,可以:
使用regex并替换:
总而言之,你会得到:
相关问题 更多 >
编程相关推荐