计算文本文件中重复单词的功能? (蟒蛇)

时间:2016-12-06 01:24:43

标签: python text counter python-3.4

以下是以下问题:

问题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'))

此外,我没有在代码中启动该部分,但我只计算重复的单词

1 个答案:

答案 0 :(得分: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)) 返回的元组,这会使代码更具可读性:

Can't convert 'int' object to str implicitly
相关问题