2013-07-05 45 views
3

大家好,我在編程初學者,我最近被賦予創建這個程序的任務,我發現它很困難。我以前設計過一個程序,用於計算用戶輸入的句子中的單詞數量,是否可以修改該程序以實現我想要的功能?用於計算的話一個打開一個文本文件的程序,計算單詞的數量並報告按文件出現在文件中的次數排序的前N個單詞?

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() 
+0

讓我們瞭解您已經第一次嘗試。這裏沒有人會爲你寫代碼。 – SolarBear

回答

5

使用collections.Counteropen()打開文件:

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回報的話基於其數:

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

要打開的文件,你可以使用open功能

from collections import Counter 
with open('input.txt', 'r') as f: 
    p = f.read() # p contains contents of entire file 
    # logic to compute word counts follows here... 

    words = p.split() 

    wordCount = len(words) 
    print "The total word count is:", wordCount 

    # you want the top N words, so grab it as input 
    N = int(raw_input("How many words do you want?")) 

    c = Counter(words) 
    for w, count in c.most_common(N): 
     print w, count 
+0

太棒了!我非常感謝輸入法!但是有一個問題,當程序問我'我想要多少單詞'時,我輸入了一個整數30,它給了我一個巨大的錯誤信息 – kmz

+0

哎呀,我修復了一個小錯誤。它現在工作嗎? – jh314

0
import re 
from collections import Counter 

with open('file_name.txt') as f: 
    sentence = f.read() 

words = re.findall(r'\w+', sentence) 
word_counts = Counter(words) 
相關問題