2016-12-06 33 views
-1

這裏是以下問題:用於統計文本文件中重複單詞的功能? (蟒蛇)

問題12

寫了一個名爲repeatWords()函數。 repeatWords()函數接受兩個字符串參數: 輸入文件的名稱和輸出文件的名稱。輸入文件僅包含小寫字母 字母和空格。 函數repeatWords()應該標識在文件 中多次出現的單詞,並將每個這樣的單詞寫入輸出文件的一行,然後寫入 單詞出現的次數。重複的單詞應該只寫入輸出文件的一行,不管 它在輸入文件中出現多少次。單詞寫入輸出文件 的順序無關緊要。 例如,如果輸入文件包含下列行:

i would not like them here or there

i would not like them anywhere

然後用下面的行的輸出文件將是正確的:

like 2

not 2

i 2

would 2

them 2

這裏是我的代碼。當我們發現計數器中的數字是否大於1(大於或等於2)時,我不知道如何獲得if語句。

def repeatWords(inFile, outFile): 
from collections import Counter 
outFile = open(outFile, 'w') 
with open(inFile, 'r') as f: 
    wordcount = Counter(f.read().split()) 
    for item in wordcount.items(): 
     if item >= 2: 
      outFile.write("{} {}".format(*item)) 
print(repeatWords('input.txt','output.txt')) 

我也沒有啓動代碼部分尚未在那裏我只算重複的話

回答

0

item是一個元組(word, count)所以不是if item >= 2:你需要比較計數:

if item[1] >= 2: 

另外,您可以通過解包返回items()元組這將使代碼位更具可讀性:

for word, count in wordcount.items(): 
    if count >= 2: 
     print("{} {}".format(word, count)) 
+0

是的,你的回答是正確的!但還有一件事:打印(「{} {} \ n」.format(word,count))是跳轉到下一行的方法。 – Jorgan

+0

@Jorgan'print'自動添加換行符,除非另有說明。你的例子在每個結果之間打印空行,注意你可以通過將'end'參數傳遞給['print']來獲得相同的結果(https://docs.python.org/3.5/library/functions.html#print) :'print(「{} {}」。format(word,count),end ='\ n \ n')' – niemmi