Search code examples
pythondictionarydefaultdict

Integers in defaultdict


I want to go through a text file and create a dictionary that has keywords and the number of times they pop up.I want it to sort of look like this:

defaultdict(<type 'int'>, {'keyword1': 1, 'keyword2': 0, 'keyword3': 3, 'keyword4': 9})

right now I get something that looks like this:

defaultdict(<type 'int'>, {'keyword1': 1})

I can print every keyword in my dictionary as it loops through though so I know it's trying something. I also know that more of these keywords should pop up and they should have instances in the text file. My code:

find_it=['keyword1', 'keyword2', 'keyword3', 'keyword4']

with open('inputfile.txt', 'r') as f:
    out = defaultdict(int)

    for key in find_it:
        counter=0
        for line in f:
            if key in line:
                out[key] += 1

my_keys=dict(**out)

What am I missing here?


Solution

  • Joran is right that a Counter is a better fit for what you're doing than a defaultdict. Here's an alternative solution:

    inputfile.txt

    The Zen of Python, by Tim Peters
    
    Beautiful is better than ugly.
    Explicit is better than implicit.
    Simple is better than complex.
    Complex is better than complicated.
    Flat is better than nested.
    Sparse is better than dense.
    Readability counts.
    Special cases aren't special enough to break the rules.
    Although practicality beats purity.
    Errors should never pass silently.
    Unless explicitly silenced.
    In the face of ambiguity, refuse the temptation to guess.
    There should be one-- and preferably only one --obvious way to do it.
    Although that way may not be obvious at first unless you're Dutch.
    Now is better than never.
    Although never is often better than *right* now.
    If the implementation is hard to explain, it's a bad idea.
    If the implementation is easy to explain, it may be a good idea.
    Namespaces are one honking great idea -- let's do more of those!
    

    count.py

    from collections import Counter
    
    find_it = {"be", "do", "of", "the", "to"}
    
    keys = Counter()
    
    with open("inputfile.txt") as f:
        for line in f:
            matches = Counter(w for w in line.split() if w in find_it)
            keys += matches
    
    print(keys)
    
    $ python count.py
    Counter({'the': 5, 'to': 5, 'be': 3, 'of': 3, 'do': 2})
    

    This finds the number of matches against find_it in each line, and adds them to the running counter keys as it goes along.

    EDIT: As pointed out by Blckknght in the comments, the previous solution missed cases where a keyword appears more than once in a line. The edited version of the code uses a slightly different method than before to overcome that problem.