Skip to content

Commit 423927f

Browse files
author
Karan Goel
committed
More idiomatic code, use defaultdict
1 parent 8f5384b commit 423927f

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

Text/count_vowels.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@
44
have it report a sum of each vowel found.
55
"""
66

7+
from collections import defaultdict
8+
79
if __name__ == '__main__':
810
string = raw_input('Enter a string: ').lower()
911

1012
vowels = ['a', 'e', 'i', 'o', 'u']
11-
counts = dict(zip(vowels, [0]*5))
13+
counts = defaultdict(int)
1214

13-
for vowel in counts:
14-
for char in string:
15-
if vowel == char:
16-
counts[vowel] += 1
15+
for char in string:
16+
if char in vowels:
17+
counts[char] += 1
1718

18-
print counts
19+
print counts.items()

0 commit comments

Comments
 (0)