一个程序可以打开一个文本文件,计算单词数并报告按单词出现在文件中的次数排序的前N个单词? [英] A program that opens a text file, counts the number of words and reports the top N words ordered by the number of times they appear in the file?

查看:253
本文介绍了一个程序可以打开一个文本文件,计算单词数并报告按单词出现在文件中的次数排序的前N个单词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我是编程的初学者,最近我被赋予创建该程序的任务,我发现这很困难.我以前设计了一个程序来计算用户输入的句子中的单词数,是否可以修改该程序以实现我想要的?

Hi all im a beginner at programming, i was recently given the task of creating this program and i am finding it difficult. I have previously designed a program that calculates the number of words in a sentence that are typed in by the user, is it possible to modify this program to achieve what i want?

import string
def main():
  print "This program calculates the number of words in a sentence"
  print
  p = raw_input("Enter a sentence: ")
  words = string.split(p)
  wordCount = len(words)
  print "The total word count is:", wordCount
main()

推荐答案

使用 collections.Counter 用于计数单词和 open()用于打开文件:

from collections import Counter
def main():
    #use open() for opening file.
    #Always use `with` statement as it'll automatically close the file for you.
    with open(r'C:\Data\test.txt') as f:
        #create a list of all words fetched from the file using a list comprehension
        words = [word for line in f for word in line.split()]
        print "The total word count is:", len(words)
        #now use collections.Counter
        c = Counter(words)
        for word, count in c.most_common():
           print word, count
main()

collections.Counter示例:

>>> from collections import Counter
>>> c = Counter('aaaaabbbdddeeegggg')

Counter.most_common 返回已排序的单词根据他们的人数:

Counter.most_common returns words in sorted order based on their count:

>>> for word, count in c.most_common(): 
...     print word,count
...     
a 5
g 4
b 3
e 3
d 3

这篇关于一个程序可以打开一个文本文件,计算单词数并报告按单词出现在文件中的次数排序的前N个单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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