0

これのいくつかの例を見つけましたが、私の質問は少し異なります。

次のような文をスキャンする必要があります

For letter in sentence
if letter == "a" or letter == "A": letterATotal = letterATotal + 1
elif letter == "e" or letter == "E": letterETotal = letterETotal + 1

等々、Uまでずっと。問題は、実際の文字を表示する方法がわからないことです。数字しか表示できません。何か案は?

4

4 に答える 4

4

これを見てください:

>>> from collections import Counter
>>> mystr = "AaAaaEeEiOoouuu"
>>> a,b = Counter(c for c in mystr.lower() if c in "aeiou").most_common(1)[0]
>>> "the most frequent vowel is {} occurring {} times".format(a.upper(), b)
'the most frequent vowel is A occurring 5 times'
>>>

についての参考資料collections.Counterです。


編集:

以下は、何が起こっているかの段階的なデモンストレーションです。

>>> from collections import Counter
>>> mystr = "AaAaaEeEiOoouuu"
>>> Counter(c for c in mystr.lower() if c in "aeiou")
Counter({'a': 5, 'o': 3, 'e': 3, 'u': 3, 'i': 1})
>>> # Get the top three most common
>>> Counter(c for c in mystr.lower() if c in "aeiou").most_common(3)
[('a', 5), ('o', 3), ('e', 3)]
>>> # Get the most common
>>> Counter(c for c in mystr.lower() if c in "aeiou").most_common(1)
[('a', 5)]
>>> Counter(c for c in mystr.lower() if c in "aeiou").most_common(1)[0]
('a', 5)
>>> a,b = Counter(c for c in mystr.lower() if c in "aeiou").most_common(1)[0]
>>> a
'a'
>>> b
5
>>>
于 2013-11-01T20:42:51.873 に答える
2

使用collections.Counter:

>>> from collections import Counter
>>> c = Counter()
>>> vowels = {'a', 'e', 'i', 'o', 'u'}
for x in 'aaaeeeeeefffddd':
    if x.lower() in vowels:
        c[x.lower()] += 1
...         
>>> c
Counter({'e': 6, 'a': 3})
>>> letter, count = c.most_common()[0]
>>> letter, count
('e', 6)
于 2013-11-01T20:34:50.080 に答える
0

次のコードは、Python 3 以降を使用している場合に機能します。

s = input ('Enter a word or sentence to find how many vowels:')

sub1 = 'a'
sub2 = 'e'
sub3 = 'i'
sub4 = 'o'
sub5 = 'u'

print ('The word you entered: ', s)

print ('The count for a: ', s.count(sub1))

print ('The count for e: ', s.count(sub2))

print ('The count for i: ', s.count(sub3))

print ('The count for o: ', s.count(sub4))

print ('The count for u: ', s.count(sub5))
于 2014-08-13T09:07:46.887 に答える
-2

文の文字列をサブストリング化し、最後に 0 ~ 4 (au の場合) のインデックスが付けられた整数の配列をインクリメントします。配列の最大値を見つけて、正しいステートメントを出力します。

于 2013-11-01T20:36:03.097 に答える