Pythonを使用してテキストファイル内の単語の頻度を数えようとしています。
次のコードを使用しています。
openfile=open("total data", "r")
linecount=0
for line in openfile:
if line.strip():
linecount+=1
count={}
while linecount>0:
line=openfile.readline().split()
for word in line:
if word in count:
count[word]+=1
else:
count[word]=1
linecount-=1
print count
しかし、私は空の辞書を取得します。「印刷カウント」は出力として {} を与える
私も使ってみました:
from collections import defaultdict
.
.
count=defaultdict(int)
.
.
if word in count:
count[word]=count.get(word,0)+1
しかし、私は再び空の辞書を取得しています。私は何が間違っているのか理解できません。誰か指摘してくれませんか?