gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 00:13:25 25 4
gpt4 key购买 nike

我是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)

关于Python 程序查找 .txt 文件中最常见的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59477691/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com