I have a question about calculating the frequency of words using dictionaries.

Asked 2 years ago, Updated 2 years ago, 20 views

f=open('c:/doit/original.txt', 'r', encoding='utf-16')

r=open('c:/doit/word frequency statistics.txt', 'w', encoding='utf-16')

data=f.read()

data2=data.split()

count={}

for word in data2:

if word in count:

    count[word] +=1

else:
    count[word] = 1

for key in count:

r.write("%s: %d" % (key,count[key])+"\n")

print("Work Complete")

r.close()

I saw this code. If you define count={} in the fifth line, there is nothing inside When we say x in y, we have an element called x in a set called y

Shouldn't there be an element called 'word' in the dictionary set?

python

2022-09-21 17:38

1 Answers

You can calculate the frequency of words as follows:

>>> Counter(["a", "b", "a"])
Counter({'a': 2, 'b': 1})

If I change the code above, I can change it like below

from collections import Counter
with open ('c:/doit/original.txt', 'r', encoding='utf-16') as f:
    data=f.read()
    data2=data.split()
    count = Counter(data2)
with open ('c:/doit/word frequency statistics.txt', 'w', encoding='utf-16') as f:
    for w,c in count.items():
        f.write("%s: %d" % (w,c)+"\n")

I was mistaken because the code was partially blocked It's a complete code.

To explain the code above, it is a code that extracts words from data 2 one by one and initializes them to 1 if the word does not exist in the count dictionary, and increases them by +1 if the word exists.


2022-09-21 17:38

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.