该程序正在尝试打印文件中最常用的10个单词。但我在打印10个最常见的单词时遇到了麻烦
from string import *
file = open('shakespeare.txt').read().lower().split()
number_of_words = 0
onlyOneWord = []
for i in file:
if i in onlyOneWord: continue
else: onlyOneWord.append(i)
lot_of_words = {}
for all_Words in onlyOneWord:
all_Words = all_Words.strip(punctuation)
number_of_words = 0
for orignal_file in file:
orignal_file = orignal_file.strip(punctuation)
if all_Words == orignal_file:
number_of_words += 1
lot_of_words[all_Words] = number_of_words
for x,y in sorted(lot_of_words.items()):
print(max(y))现在,它将打印完整文件中的内容
我需要它来打印像这样的10个最常见的单词,并让它运行得更快
苹果: 251,苹果: 234等。
https://stackoverflow.com/questions/47612928
复制相似问题