repeatCount 函数没有给我正确的答案。为什么会这样?

repeatCount function does not give me the right answer. Why does this happen?

我以前使用 Counter 为家庭作业做过这道题。现在,我正在为决赛研究同样的问题。我想记住字典而不是这次决赛的计数器。我试着用字典来解决这个问题。

所以问题是创建函数名称 repeatCount。该函数的目的是读取输入文件的每一行,确定该行中出现多次的单词数,并将该数字写入输出文件中的一行。

输入文件文本是这样的:

Woke up this morning with an ache in my head
I splashed on my clothes as I spilled out of bed
I opened the window to listen to the news
But all I heard was the Establishment Blues

我的输出文件应该是这样的:

0
2
3
2

正确的输出是:

0
1
2 
0

现在这是我的代码。我的代码的哪个特定部分导致 Python 产生错误答案?:

def repeatCount(inFile, outFile):
    inF = open(inFile, 'r')
    outF = open(outFile, 'w')

    d = {}
    for line in inF.readlines():
        count = 0
        words = line.split()
        for word in words:
            if word not in d:
                d[word] = 1
            elif word in d:
                d[word] += 1
            if d[word] > 1:
                count += 1
        outF.write(str(count) + "\n")

print(repeatCount('inputFile.txt', 'outputFile.txt'))

如果您为每一行重新设置字典,您的程序将开始为您提供正确的输出。 IE。将 d = {} 移动到外部 for 循环的内部。然后它将适用于您当前的输入。但是您的内部 for 循环仍然存在问题,因为它没有忽略已经计算在内的重复单词。再试一次,向我们展示您的下一次迭代!

根据 @gipsy

的建议

在 for 循环中移动声明字典。还可以使用列表内置函数 count 来获取单词的实际计数。

d = {}

您的代码的修改版本。

def repeatCount(inFile, outFile):
    inF = open(inFile, 'r')
    outF = open(outFile, 'w')
    for line in inF.readlines():
        d = {}
        count = 0
        words = line.split()
        for word in words:
            if word not in d:
                wc = words.count(word)
                d[word] = 1
                if wc > 1:
                    count += 1
        outF.write(str(count) + "\n")

print(repeatCount('inputFile.txt', 'outputFile.txt'))

您还可以改进您的代码。参考 Python: count frequency of words in a list