Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 统计文件中出现的单词数_Python_File_Dictionary - Fatal编程技术网

Python 统计文件中出现的单词数

Python 统计文件中出现的单词数,python,file,dictionary,Python,File,Dictionary,我想使用字典计算文件中每个单词的出现次数(文件中包含的所有单词都是小写,文件中不包含任何标点符号) 我想优化我的代码,因为我知道列表需要不必要的时间 def create_dictionary(filename): d = {} flat_list = [] with open(filename,"r") as fin: for line in fin: for word in line.split():

我想使用字典计算文件中每个单词的出现次数(文件中包含的所有单词都是小写,文件中不包含任何标点符号)

我想优化我的代码,因为我知道列表需要不必要的时间

def create_dictionary(filename):
    d = {}
    flat_list = []
    with open(filename,"r") as fin:
        for line in fin:
            for word in line.split():
                flat_list.append(word)
        for i in flat_list:
            if d.get(i,0) == 0:
                d[i] = 1
            else :
                d[i] +=1

        return d
例如,包含以下内容的文件:

i go to the market to buy some things to 
eat and drink because i want 
to eat and drink
应返回:

{'i': 2, 'go': 1, 'to': 4, 'the': 1, 'market': 1, 'buy': 1, 'some': 1, 'things': 1, 'eat': 2, 'and': 2, 'drink': 2, 'because': 1, 'want': 1}

我可以改进什么?

只需使用集合。计数器:

with open(filename,"r") as fin:
    print(Counter(fin.read().split()))
对那种事情来说很好。