计算句子中的元音数量并显示最频繁的 [英] Count the amount of vowels in a sentence and display the most frequent
本文介绍了计算句子中的元音数量并显示最频繁的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
到目前为止,这是我计算元音的代码。我需要浏览一个句子,计数和比较元音,然后显示出现次数最多的元音。
This is my code so far for counting vowels. I need to to scan through a sentence, count and compare the vowels and then display the top occurring vowels.
from collections import Counter
vowelCounter = Counter()
sentence=input("sentence")
for word in sentence:
vowelCounter[word] += 1
vowel, vowelCount= Counter(vowel for vowel in sentence.lower() if vowel in "aeiou").most_common(1)[0]
有人有更好的方法吗?
推荐答案
IMO,为避免出现长行,最好避免清晰度:
IMO, long lines are best avoided for the sake of clarity:
#!/usr/local/cpython-3.3/bin/python
import collections
sentence = input("sentence").lower()
vowels = (c for c in sentence if c in "aeiou")
counter = collections.Counter(vowels)
most_common = counter.most_common(1)[0]
print(most_common)
这篇关于计算句子中的元音数量并显示最频繁的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文