小编use*_*103的帖子

计算单词频率并从中制作字典

我想从文本文件中获取每个单词,并在字典中计算单词频率.

例: 'this is the textfile, and it is used to take words and count'

d = {'this': 1, 'is': 2, 'the': 1, ...} 
Run Code Online (Sandbox Code Playgroud)

我不是那么远,但我只是看不出如何完成它.我的代码到目前为止:

import sys

argv = sys.argv[1]
data = open(argv)
words = data.read()
data.close()
wordfreq = {}
for i in words:
    #there should be a counter and somehow it must fill the dict.
Run Code Online (Sandbox Code Playgroud)

python dictionary count readlines

6
推荐指数
3
解决办法
5万
查看次数

标签 统计

count ×1

dictionary ×1

python ×1

readlines ×1