Python txt 文件字数统计程序

标签 python python-3.5

我正在尝试编写一个程序来计算 txt 文件中 5 个最常见的单词。

这是我到目前为止所拥有的:

file = open('alice.txt')
wordcount = {}

for word in file.read().split():
    if word not in wordcount:
        wordcount[word] = 1
    else:
        wordcount[word] += 1

for k, v in wordcount.items():
    print (k, v)

程序本身会计算 .txt 文件中的每个单词。

我的问题是如何使其只计算文件中 5 个最常见的单词,以便显示这些单词以及每个单词旁边的单词计数。

有一个问题 - 我不能使用字典......无论这意味着什么。

最佳答案

很简单,您只需找到文件中最常见的 5 个单词

所以你可以这样做:

wordcount = sorted(wordcount.items(), key=lambda x: x[1], reverse=True)

然后,该字典将按值排序(请记住,sorted 返回一个列表)。

您可以使用以下代码获取 5 个最常见的单词:

for k, v in wordcount[:5]):
    print (k, v)

所以完整的代码如下:

wordcount = {}

with open('alice.txt') as file:  # with can auto close the file
    for word in file.read().split():
        if word not in wordcount:
            wordcount[word] = 1
        else:
            wordcount[word] += 1

wordcount = sorted(wordcount.items(), key=lambda x: x[1], reverse=True)

for k, v in wordcount[:5]:
    print(k, v)

另外,这里有一个更简单的方法来执行此操作使用 collections.Counter :

from collections import Counter
with open('alice.txt') as file:  # with can auto close the file
    wordcount = Counter(file.read().split())

for k, v in wordcount.most_common(5):
    print(k, v)

输出与第一个解决方案相同。

关于Python txt 文件字数统计程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33384673/

相关文章:

python - OpenCV,Python : How to use mask parameter in ORB feature detector

python - 替换列表中的字符串值

python - 如何在 Python 中将单元素列表完美转换为元组?

python - Keras 分类器的准确度在训练期间稳步上升,然后下降到 0.25(局部最小值?)

django - 属性错误: 'module' object has no attribute 'lru_cache'

python - Pandas.read_csv : backslash delimiter required?

java - 在将对象从 Java 传递到 Jython 时保留对象的 Java 类型

python 请求和 cx_freeze

python - 使用 python 将粘性位分配给 linux 组权限

python - 可以使用Python 3.5.2下载OpenCV吗?