該程序試圖在文件中打印最常用的10個單詞。但我無法打印的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 等
請格式化您的代碼。 – enyard
如果我理解你的問題,你只需要使用不同的方法打印看看這個:https://stackoverflow.com/questions/7197315/5-maximum-values-in-a-python-dictionary關於製作它運行得更快,你需要優化你的算法(即遍歷整個文件的次數更少)。 – DoesData