まずは、辞書にvowels
を作ってみよう。我々は最初のループで行う試合を保持するために、第2のいずれかが必要になります。
s = "apples"
vowels = dict.fromkeys('aeiou', 0)
matches = {}
私たちは、対応するキー(母音の値をインクリメントする少しごfor
ループを変更する必要があります):
for char in s:
if char in vowels:
vowels[char] += 1
char
が母音(または単に置くかどうかをチェックし、上記for
ループは、vowels
に見られる鍵)の一つです。たとえば、char
が "a"だった場合、if
ステートメントはTrueを返し、キー( "a")の値(コロンの後の整数)は次のように増加します。 1。その後、新しい値を代入し
for vowel in vowels:
if vowels[vowel] < 1: # The vowel didn't appear in the word
continue
else:
matches[str(vowel)] = vowels[vowel]
最後の行がmatches
辞書(matches[str(vowel)]
部分)のための新しいキーを作成します。今、私たちが必要とするすべてはmatches
辞書にすべてのキー、値が0以上を置くことですvowels
辞書(= vowels[vowel]
部分)のそれぞれのキーの値に等しいキーを入力します。
print matches
全コード:
count = 0
s = "apple"
vowels = dict.fromkeys('aeiou', 0)
matches = {}
for char in s:
if char in vowels:
vowels[char] += 1
for vowel in vowels:
if vowels[vowel] < 1:
continue
else:
matches[str(vowel)] = vowels[vowel]
print matches
どの辞書を取得しようとしていますか?母音から出現するまでの時間数? – Mureinik