简体   繁体   中英

Counting how many times each vowel appears

I've written a little program to count how many times each vowel appears in a list, but it's not returning the correct count, and I can't see why:

vowels = ['a', 'e', 'i', 'o', 'u']
vowelCounts = [aCount, eCount, iCount, oCount, uCount] = (0,0,0,0,0)
wordlist = ['big', 'cats', 'like', 'really']

for word in wordlist:
    for letter in word:
        if letter == 'a':
            aCount += 1
        if letter == 'e':
            eCount += 1
        if letter == 'i':
            iCount += 1
        if letter == 'o':
            oCount += 1
        if letter == 'u':
            uCount += 1
for vowel, count in zip(vowels, vowelCounts):
    print('"{0}" occurs {1} times.'.format(vowel, count))

The output is

"a" occurs 0 times.
"e" occurs 0 times.
"i" occurs 0 times.
"o" occurs 0 times.
"u" occurs 0 times.

However, if I type aCount in the Python shell, it gives me 2 , which is correct, so my code has indeed updated the aCount variable and stored it correctly. Why isn't it printing the correct output?

The problem is with this line:

vowelCounts = [aCount, eCount, iCount, oCount, uCount] = (0,0,0,0,0)

vowelCounts does not get updated if you start incrementing aCount later.

Setting a = [b, c] = (0, 0) is equivalent to a = (0, 0) and [b, c] = (0, 0) . The latter is equivalent to setting b = 0 and c = 0 .

Reorder your logic as below and it will work:

aCount, eCount, iCount, oCount, uCount = (0,0,0,0,0)

for word in wordlist:
    for letter in word:
        # logic 

vowelCounts = [aCount, eCount, iCount, oCount, uCount]

for vowel, count in zip(vowels, vowelCounts):
    print('"{0}" occurs {1} times.'.format(vowel, count))

You can also use collections counter (which is the natural go-to function when counting things, it returns a dictionary):

from collections import Counter

vowels = list('aeiou')
wordlist = ['big', 'cats', 'like', 'really']

lettersum = Counter(''.join(wordlist))

print('\n'.join(['"{}" occurs {} time(s).'.format(i,lettersum.get(i,0)) for i in vowels]))

Returns:

"a" occurs 2 time(s).
"e" occurs 2 time(s).
"i" occurs 2 time(s).
"o" occurs 0 time(s).
"u" occurs 0 time(s).

lettersum:

Counter({'l': 3, 'a': 2, 'e': 2, 'i': 2, 'c': 1, 'b': 1, 
         'g': 1, 'k': 1, 's': 1, 'r': 1, 't': 1, 'y': 1})

You can use a dictionary comprehension:

vowels = ['a', 'e', 'i', 'o', 'u']
wordlist = ['big', 'cats', 'like', 'really']
new_words = ''.join(wordlist)
new_counts = {i:sum(i == a for a in new_words) for i in vowels}

Output:

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

In addition to what @jpp said simple datatypes like integers are returned by value not by reference so when you assign it to something and change that something it doesn't get affected

a = 10
b = a #b=a=10
b = 11 #b=11, a=10
print a, b
--> 10 11

I'd have made this a comment to his but I need reputation to do that :D

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM