Python Script: Find most occured alphabet in a text

How to find the most occurred alphabet in a text? Below is how :


import collections, string
def occurence(text):
    alpha = string.lowercase
    text = text.lower()
    count = {}
    final = []
    for x in alpha:
        localcount = text.count(x)
        count[x] = localcount
    max1 = max(count, key=count.get)

    a = count[max1]

    for y in count:
        if count[y] == a:
            final.append(y)

    return min(final)

=============================================

Note :

1. If two alphabet have safe frequency, it will return the alphabet which is earlier in alphabetical order.

2. Works even if input is in mixed case.


Comments