Python 3.0 - How do I output which character is counted the most?

102 Views Asked by At

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()
5

There are 5 best solutions below

0
On BEST ANSWER

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.

0
On

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']
1
On

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).

4
On

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()
0
On

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())]))