Home > database >  Turning a list into a frequency dictionary
Turning a list into a frequency dictionary

Time:11-16

I am currently attempting to turn a list into a frequency dictionary. I am reading a file, separating the file into each individual words on a line and attempting to turn each word into its own frequency dictionary in order to find how many times it occurs. I was wondering how I would accomplish this. This is what I currently have:

with open(file, 'r', encoding = 'utf-8') as fp:
    lines = fp.readlines()
    for row in lines:
        for word in row.split():
            print(word)

Currently, my program outputs a new word on each line. How would I make it so that the words are each their own dictionaries and can find the frequency of them?

CodePudding user response:

The Counter class was designed for exactly this task.

from collections import Counter
with open(file, 'r', encoding='utf-8') as fp:
    counts = Counter(fp.read().split())

Now you can print counts and use its methods to get the most common words.

CodePudding user response:

If you are doing this for learning purposes (i.e. want to do it yourself, rather than using Counter, here is an example:

d = {} # Start with an empty ditctionary
with open(file, 'r', encoding = 'utf-8') as fp:
    lines = fp.readlines()
    for row in lines:
        for word in row.split():
            d[word] = d.get(word,0)   1 # Insert word into dictionary or update its value
  • Related