在.txt文件中查找最常用单词的Python程序

在.txt文件中查找最常用单词的Python程序,python,python-3.x,Python,Python 3.x,我是python新手 我有一个文本文件 我想让程序做的是找到最常用的20个单词,并显示它们重复了多少次(您可以使用Python的内置函数和库。不要使用不同的库) 接下来我该怎么办 我只能做这部分 FILE_NAME = 'file.txt' wordCounter = {} with open(FILE_NAME,'r') as fh: for line in fh: word_list = line.replace(',','').replace('\'','')

我是python新手 我有一个文本文件 我想让程序做的是找到最常用的20个单词,并显示它们重复了多少次(您可以使用Python的内置函数和库。不要使用不同的库) 接下来我该怎么办

我只能做这部分

FILE_NAME = 'file.txt'

wordCounter = {}

with open(FILE_NAME,'r') as fh:
    for line in fh:

        word_list = line.replace(',','').replace('\'','').replace('.','').lower().split()
        for word in word_list:
            if word not in wordCounter:
                wordCounter[word] = 1
            else:
                wordCounter[word] = wordCounter[word] + 1

for  (word,occurance)  in wordCounter.items(): 
    print(word,occurance)

您可以像这样使用集合:

from collections import Counter

f= open("file.txt","r")
text =f.read()
#clean data
for char in '-.,\n':
    text=text.replace(char,' ')
#to lower
text = text.lower()
#to list
ls = text.split()
#most_common = 20
top_20 = Counter(ls).most_common(20)
#loop all most_common 
for x in top_20:
  print(x)  
('dynamic', 3)
('as', 3)
('high', 2)
('level', 2)
('with', 2)
('for', 2)
('python', 1)
('is', 1)
('an', 1)
('interpreted', 1)
('object', 1)
('oriented', 1)
('programming', 1)
('language', 1)
('semantics', 1)
('its', 1)
('built', 1)
('in', 1)
('data', 1)
('structures', 1)
结果与此类似:

from collections import Counter

f= open("file.txt","r")
text =f.read()
#clean data
for char in '-.,\n':
    text=text.replace(char,' ')
#to lower
text = text.lower()
#to list
ls = text.split()
#most_common = 20
top_20 = Counter(ls).most_common(20)
#loop all most_common 
for x in top_20:
  print(x)  
('dynamic', 3)
('as', 3)
('high', 2)
('level', 2)
('with', 2)
('for', 2)
('python', 1)
('is', 1)
('an', 1)
('interpreted', 1)
('object', 1)
('oriented', 1)
('programming', 1)
('language', 1)
('semantics', 1)
('its', 1)
('built', 1)
('in', 1)
('data', 1)
('structures', 1)

欢迎来到stack overflow,这不是一个家庭作业网站。问题应该包括解决问题的诚实尝试。查看更多。请提供您已经尝试过的内容,以便我们能够帮助您。好的,我会编辑它。我认为您几乎做到了。。检查您的缩进是否正确?您只需要根据值对字典进行排序