A string frequency counter

Posted by Antonio Catalano on April 10, 2018
Provide for the worst; the best can take care of itself
Yiddish proverb
Python 3 Code
     
def count_char():
     word = input ('Write word or sentence: ')
     dizio = dict()
     for i in word:
         if i not in dizio:
              dizio[i] = 1
         else:
              dizio[i] += 1
     return dizio

print(count_char())
     

What does this function do? Simple: it takes an input string (word or sentence or number, it doesn't matter: the input function returns always a string type) and creates a dictionary where as keys we have the characters of the input, and as values the number of repetitions of that character. If for example we give this input from keyboard 'A simple test regarding the input function and the dictionary object', output will be:

Write word or sentence: A simple test regarding the input function and the dictionary object
{'A': 1, ' ': 10, 's': 2, 'i': 6, 'm': 1, 'p': 2, 'l': 1, 'e': 6, 't': 8, 'r': 3, 'g': 2, 'a': 3, 'd': 3, 'n': 6, 'h': 2, 'u': 2, 'f': 1, 'c': 3, 'o': 3, 'y': 1, 'b': 1, 'j': 1}
As you can notice a whitespace is a character like any other, and 'A' and 'a' are two different characters. Ok, now we want to sort the items of our output in an increasing value in order to have a clearer picture of the frequency counter. We can think about dictionary as a multiplicity of couples in the form of (key, value). If we want to sort by value we must first change the form of the couples: from (key, value) into (value, key). We can do this through a list comprehension. Below an example in code:

new_couples = [(value, key) for (key, value) in mydict.items()]

Where mydict is our dictonary object and mydict.items() returns the view object of the couples of the list, creating an iterable object. Now the last step: call the sorted function where the argument is new_couples list. The sorted() method sorts the element of a given iterable in a specific order. Without specifying any further, this method sorts the argument in the ascending order of its own first element. Instead we will sort in descending order through setting parameter reverse = True . So, let's put all pieces togheter:


Final code

def count_char():
    word = input ('Write word or sentence: ')
    dizio = dict()
    for i in word:
        if i not in dizio:
            dizio [i] = 1
        else:
            dizio [i] = dizio[i] + 1
    return dizio

mydict = count_char()

new_x_List = [(value,key) for (key,value) in mydict.items()]

for i,k in sorted(new_x_List, reverse = True):
    print(k, '-->', i)


Output:

Write word or sentence: A simple test regarding the input function and the dictionary object

  --> 10
t --> 8
n --> 6
i --> 6
e --> 6
r --> 3
o --> 3
d --> 3
c --> 3
a --> 3
u --> 2
s --> 2
p --> 2
h --> 2
g --> 2
y --> 1
m --> 1
l --> 1
j --> 1
f --> 1
b --> 1
A --> 1
We could achieve the same result in a faster way by exploiting the module Counter from collections. I invite you to check it out for a more pythonic and compact code.