Counting letter using the python language

  • comments: true
  • badges: true
from collections import Counter

fname = "Wordle_MysteryWords.csv"

def readfile(fname):
    '''Reads a file and return the contents as a string'''
    f = open(fname, 'r')  
    text = f.read()       
    f.close()             
    return text    
def count_letters(words):
    '''Returns a dictionary with letters and their counts'''
    pass
    d = {}
    for word in words:
        for char in word:
            if char not in d:
                d[char] = 1
            else:
                d[char] += 1
    return d
def count_words(words):
    '''Returns a dictionary with letters and the number of words each
       letter occurs in'''
    d = {}
    for word in words:
        chars = set(word)
        for char in chars:
            if char not in d:
                d[char] = 1
            else:
                d[char] += 1
    return d
def count_mult(words):
    '''Returns a dictionary with the number of times a letter
       occurs multiple times in a word'''
    d = {}
    for word in words:
        c = Counter(word)
        for char in c:
            if c[char] > 1:
                if char not in d:
                    d[char] = 1
                else:
                    d[char] += 1
    return d
def count_pos(words):
    '''Returns a dictionary with the number of times each letter occurs
       in each position in a word'''
    d = {}
    for word in words:
        for i in range(len(word)):
            char = word[i]
            if char not in d:
                d[char] = [0] * 5
            d[char][i] += 1 
    return d
def count_letters2(words):
    '''Use a Counter; functionally the same as count_letters()'''
    c = Counter()
    for word in words:
        c.update(word)
    return c
def show_counts(d, title):
    '''Print the keys and values from dictionary d'''
    print(title)
    keylist = list(d)
    keylist.sort()
    for key in keylist:
        print(key, d[key])
def main():
    wordstr = readfile(fname)
    print(wordstr[:30])  
    wordlist = wordstr.split()  
    print(wordlist[:20])  
    print(wordlist[-20:])
    print(count_letters(wordlist), "Number of times each letter occurs")
    print(count_letters2(wordlist), "Number of times each letter occurs")
    print(count_words(wordlist), "Number of words each letter occurs in")
    print(count_mult(wordlist), "Counts of letters in multiple words")
    print(count_pos(wordlist),"Number of times each letter occurs in each position")
main()
abhor
abide
abled
abode
abort

['abhor', 'abide', 'abled', 'abode', 'abort', 'about', 'above', 'abuse', 'abyss', 'acorn', 'acrid', 'actor', 'acute', 'adage', 'adapt', 'adept', 'admin', 'admit', 'adobe', 'adopt']
['wrath', 'wreak', 'wreck', 'wrest', 'wring', 'wrist', 'write', 'wrong', 'wrote', 'wrung', 'wryly', 'yacht', 'yearn', 'yeast', 'yield', 'young', 'youth', 'zebra', 'zesty', 'zonal']
{'a': 971, 'b': 274, 'h': 389, 'o': 753, 'r': 899, 'i': 671, 'd': 393, 'e': 1230, 'l': 719, 't': 727, 'u': 467, 'v': 153, 's': 668, 'y': 424, 'c': 476, 'n': 575, 'g': 311, 'p': 367, 'm': 316, 'f': 230, 'x': 37, 'w': 195, 'k': 209, 'z': 40, 'j': 27, 'q': 29} Number of times each letter occurs
Counter({'e': 1230, 'a': 971, 'r': 899, 'o': 753, 't': 727, 'l': 719, 'i': 671, 's': 668, 'n': 575, 'c': 476, 'u': 467, 'y': 424, 'd': 393, 'h': 389, 'p': 367, 'm': 316, 'g': 311, 'b': 274, 'f': 230, 'k': 209, 'w': 195, 'v': 153, 'z': 40, 'x': 37, 'q': 29, 'j': 27}) Number of times each letter occurs
{'a': 904, 'o': 672, 'r': 837, 'b': 262, 'h': 379, 'i': 647, 'e': 1053, 'd': 370, 'l': 648, 't': 665, 'u': 457, 'v': 149, 's': 617, 'y': 416, 'n': 550, 'c': 447, 'g': 300, 'p': 346, 'm': 298, 'f': 207, 'x': 37, 'w': 194, 'k': 201, 'z': 35, 'j': 27, 'q': 29} Number of words each letter occurs in
{'s': 49, 'a': 67, 'f': 22, 'o': 81, 'g': 11, 'e': 172, 'i': 24, 'l': 71, 'n': 23, 'p': 19, 'r': 60, 't': 61, 'u': 10, 'b': 11, 'd': 22, 'c': 29, 'm': 15, 'y': 8, 'z': 5, 'h': 10, 'k': 8, 'v': 4, 'w': 1} Counts of letters in multiple words
{'a': [136, 304, 304, 163, 64], 'b': [173, 11, 55, 24, 11], 'h': [69, 144, 9, 28, 139], 'o': [41, 279, 244, 131, 58], 'r': [105, 267, 163, 152, 212], 'i': [34, 202, 266, 158, 11], 'd': [111, 20, 75, 69, 118], 'e': [72, 242, 177, 317, 422], 'l': [88, 201, 112, 162, 156], 't': [149, 77, 111, 138, 252], 'u': [33, 186, 165, 82, 1], 'v': [43, 15, 49, 46, 0], 's': [366, 16, 80, 170, 36], 'y': [6, 23, 29, 3, 363], 'c': [198, 40, 56, 151, 31], 'n': [37, 87, 139, 182, 130], 'g': [115, 12, 67, 76, 41], 'p': [142, 61, 58, 50, 56], 'm': [107, 38, 61, 68, 42], 'f': [136, 8, 25, 35, 26], 'x': [0, 14, 12, 3, 8], 'w': [83, 44, 26, 25, 17], 'k': [20, 10, 12, 55, 112], 'z': [3, 2, 11, 20, 4], 'j': [20, 2, 3, 2, 0], 'q': [23, 5, 1, 0, 0]} Number of times each letter occurs in each position