2016-11-14 43 views
0

我正在尋找一個程序,在其中輸入一個單詞或短語,並計算輸入中元音的數量,然後返回每個元音的數量。我已經這樣做了,但是我的代碼很長,我想知道如何更有效地編寫它。更有效地使用循環

如何減少循環次數?

text = input("enter string:") 

vowels = ["a","e","i","o","u"] 
count = 0 
for letters in text: 
    if letters in vowels: 
     count = count + 1 


print ("vowel count is:",count) 
numA = 0 
numE = 0 
numI = 0 
numO = 0 
numU = 0 

for a in text: 
    if a in "a": 
     numA = numA + 1 

for e in text: 
    if e in "e": 
     numE = numE + 1 

for i in text: 
    if i in "i": 
     numI = numI + 1 

for o in text: 
    if o in "o": 
     numO = numO + 1 

for u in text: 
    if u in "u": 
     numU = numU + 1 




print ("a occurs",numA) 
print ("e occurs",numE) 
print ("i occurs",numI) 
print ("o occurs",numO) 
print ("u occurs",numU) 

回答

6

使用dict

vowels = ["a", "e", "i", "o", "u"] 
# We use a dict to count the vowels. 
# It is initialized with 0 for every vowel. 
count = {v: 0 for v in vowels} 

# Loop over every character in the input. 
for c in "foo bar baz": 
    # If the character is a vowel, increment its count. 
    if c in vowels: 
     count[c] = count[c] + 1 

print(count) 

輸出:

{'i': 0, 'u': 0, 'a': 2, 'o': 2, 'e': 0} 
+0

你能解釋一下? – makavelllli

+0

@makavelllli我已添加一些評論。 – 2016-11-14 12:04:10

0

您可以使用count()

number_of_a = text.count('a') 
number_of_e = text.count('e') 
... 

和元音總數:

number_of_vowels = sum(1 for x in text if x in 'aeiou') 
0

我喜歡上面顯示的詞典的例子,但如果你願意,你可以做以下的微小變化:

  1. 只有遍歷輸入字符串一次。
  2. 使用數組跟蹤每個元音的計數。

    vowelArray = [0, 0, 0, 0, 0] 
    inputStr = "This is the string to loop over" 
    
    for char in inputStr: 
        if char == "a": 
         vowelArray[0] += 1 
        if char == "e": 
         vowelArray[1] += 1 
        if char == "i": 
         vowelArray[2] += 1 
        if char == "o": 
         vowelArray[3] += 1 
        if char == "u": 
         vowelArray[4] += 1 
    
    print(vowelArray) 
    
+0

好主意只能迭代一次字符串,但是您應該使用collections.Counter來代替,正如我的答案中所示,它既簡單又簡短。 – Guillaume

1

在補充@Lutz喇叭的答案,你可以把它短得多,而且也算不同的元音的數量,因爲你需要:

text = input("enter string:") 
# counts the number of different vowels 
no_vowels = len(set(text).intersection("aeiou")) 
# this is a dict comprehension, it constructs a dict with a vowel as key and the occurrence count as key 
counts = {vowel: text.count(vowel) for vowel in "aeiou"} 

# then you can print the number of occurrences from each item 
print("vowel count is:", no_vowels) 
for vowel in "aeiou": 
    print(vowel, "is used", count[vowel], "times") 

或者你也可以減少4個讀取行,如果你不需要保持計數的變量,但只需要打印出來:

text = input("enter string:") 
print("vowel count is", len(set(text).intersection("aeiou"))) 
for vowel in "aeiou": 
    print(vowel, "occurs", text.count(vowel), "times") 

或者你可以使用Python b uilt式collections.Counter它同時提供最佳的性能和功能:

from collections import Counter 
text = input("enter string:") 
counter = Counter(text) 

# counter now contains the counts for all letters, not only vowels 
for vowel in "aeiou": 
    print(vowel, "occurs", counter[vowel], "times") 

# this one is trickier, it is a sum of boolean, which returns the number of True 
print("vowel count is:", sum(vowel in counter for vowel in "aeiou")) 
0

試圖保留原始代碼儘可能地,這裏是你的算法,使用dict - 數元音:

#text = input("enter string:") 
text = "phraseanditcountsthenumberofvowels" 

vowels = {"a":0,"e":0,"i":0,"o":0,"u":0} 
for letter in text: 
    letter = letter.lower() 
    if letter in vowels: 
     vowels[letter] += 1 

print(vowels) 
+0

謝謝。我還想知道,如果我輸入「土豆」這個詞,它有2個「o」和1個「a」。我如何製作它,以便它只打印最不頻繁的元音,所以在這種情況下「a」?我試圖使用min(但它只打印了0)。 – makavelllli

+0

只給min()只給大於0的對象,@makavelllli。另外,你可能想閱讀http://stackoverflow.com/help/why-vote。 – boardrider