来自txt文件的Python字数统计程序 [英] Python word count program from txt file

查看:84
本文介绍了来自txt文件的Python字数统计程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to write a program that counts the 5 most common words in a txt file.

这是我目前所拥有的:

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 文件中的每个单词.

The program as it is counts every word in the .txt file.

我的问题是如何使它只计算文件中最常见的 5 个单词,以便在每个单词旁边显示单词和单词计数.

My question is how to make it so it only counts the 5 most common words in the file so that it displays the words and the word count next to each word.

一个问题 - 我不能用字典……不管那是什么意思.

One catch - I can't use dictionary...whatever that means.

推荐答案

简单,你只需要找到文件中最常用的 5 个词.

所以你可以这样做:

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

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

And then, this dictionary will be sorted by values(remember that sorted return a list).

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

You can use the following code to get the 5 most common words:

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

<小时>

所以完整的代码如下:


So the full code looks like:

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)

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

The output is same as the first solution.

这篇关于来自txt文件的Python字数统计程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆