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
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.
© 2025 OneMinuteCode. All rights reserved.