1

So I was able to create a program that counts the amount of vowels (specifially e i o) in a text file I have on my computer. However, I can't figure out for the life of me how to show which one occurs the most. I assumed I would say something like

for ch in 'i':
    return numvowel?

I'm just not too sure what the step is. I basically want it to output in the end saying "The letter, i, occurred the most in the text file"

def vowelCounter():
    inFile = open('file.txt', 'r')
    contents = inFile.read()

    # variable to store total number of vowels
    numVowel = 0

    # This counts the total number of occurrences of vowels o e i.
    for ch in contents:
        if ch in 'i':
            numVowel = numVowel + 1
        if ch in 'e':
            numVowel = numVowel + 1    
        if ch in 'o':
            numVowel = numVowel + 1

    print('file.txt has', numVowel, 'vowel occurences total')
    inFile.close()

vowelCounter()
Mohammed Aouf Zouag
  • 17,042
  • 4
  • 41
  • 67
Jim Derkin
  • 69
  • 1
  • 1
  • 9

5 Answers5

3

If you want to show which one occurs the most, you have to keep counts of each individual vowel instead of just 1 total count like what you have done.

Keep 3 separate counters (one for each of the 3 vowels you care about) and then you can get the total by summing them up OR if you want to find out which vowel occurs the most you can simply compare the 3 counters to find out.

shafeen
  • 2,431
  • 18
  • 23
1

Try using regular expressions; https://docs.python.org/3.5/library/re.html#regular-expression-objects

import re

def vowelCounter():

    with open('file.txt', 'r') as inFile:

        content = inFile.read()

        o_count = len(re.findall('o',content))
        e_count = len(re.findall('e',content))
        i_count = len(re.findall('i',content))

        # Note, if you want this to be case-insensitive,
        # then add the addition argument re.I to each findall function

        print("O's: {0}, E's:{1}, I's:{2}".format(o_count,e_count,i_count))

vowelCounter()
Nick
  • 7,103
  • 2
  • 21
  • 43
  • @bgusach - And yet it works and provides another avenue for the OP to explore. Had you actually written a technical critique of this solution and why you think it is not a good one, that would have been constructive. Instead, you decided to write a meaningless quip for what reason? – Nick Feb 15 '16 at 15:45
  • I thought the technical critique was quite clear, but let's make explicit: there is absolutely no need to use such a complex tool as regular expressions if the goal is just count how many times a single letter appears in a string. It adds an innecessary level of complexity and brings nothing: regexps have to be compiled, they need more time to be applied and in this case they make looping three times over the text necessary. And most importantly, since the OP is clearly new to programming, I am pretty sure it is the wrong approach to add yet another new concept. Cheers. – bgusach Feb 15 '16 at 16:17
  • Who are you to determine how much or which concepts are too much to share with OP? I am 100% fine with you adding the technical comments to this as it gives perspective; however, what's so wrong with showing alternative solutions?? – Nick Feb 15 '16 at 16:21
  • Your solution is neither technically nor didactically a good answer, therefore I think it does not help the OP. Just wanted to comment it in a friendly and constructive way. You are free to take it as you want. Regards. – bgusach Feb 15 '16 at 16:49
1

You can do this:

vowels = {} # dictionary of counters, indexed by vowels

for ch in contents:
    if ch in ['i', 'e', 'o']:
        # If 'ch' is a new vowel, create a new mapping for it with the value 1
        # otherwise increment its counter by 1
        vowels[ch] = vowels.get(ch, 0) + 1

print("'{}' occured the most."
    .format(*[k for k, v in vowels.items() if v == max(vowels.values())]))
Mohammed Aouf Zouag
  • 17,042
  • 4
  • 41
  • 67
1

Python claims to have "batteries included", and this is a classical case. The class collections.Counter does pretty much this.

from collections import Counter

with open('file.txt') as file_
    counter = Counter(file_.read())

print 'Count of e: %s' % counter['e']
print 'Count of i: %s' % counter['i']
print 'Count of o: %s' % counter['o']
bgusach
  • 14,527
  • 14
  • 51
  • 68
0

Let vowels = 'eio', then

{ i: contents.count(i) for i in vowels }

For each item in vowels count the number of occurrences in contents and add it as part of the resulting dictionary (note the wrapping curly brackets over the comprehension).

elm
  • 20,117
  • 14
  • 67
  • 113