现在,我的代码打印了txt文件中每个单词的使用次数。我试图让它只打印前三个字与大写字母在txt文件中使用。。。你知道吗
file=open("novel.txt","r+")
wordcount={}
for word in file.read().split():
if word not in wordcount:
wordcount[word] = 1
else:
wordcount[word] += 1
for a,b in wordcount.items():
print (b, a)
Tags:
在
Collections
中有一个Counter
类。https://docs.python.org/2/library/collections.html。你知道吗首先,您要使用^{} 将结果限制为仅大写的单词:
然后sort the results使用^{} 打印出前三个:
相关问题 更多 >
编程相关推荐