在列表上调用计数器函数时出错



我正在编写一个程序来计算txt文件中出现最多的20个单词。当我剥离并计数一个文件时,该程序工作正常,但是当我输入两个要剥离和计数的文件时,我收到一条错误消息,说"'计数器'对象不可调用"。我很困惑,因为这再次适用于一个文档。下面是我的代码,错误来自 while 循环。谢谢!

 from collections import Counter
 numOfData = int(input("How many documents would you liek to scan? "))
 i = 0
 displayedI = str(i)
docList = []
finalData = []
##Address of document would take 'test.txt' for example
while i < numOfData:
    newAddress = input("Address of document " + displayedI + ": ")
    docList.append(newAddress)
    i += 1
 print(docList)
 indexList = 0

 for x in docList:
     file = open(docList[indexList], 'r')
     data_set = file.read().strip()
     file.close()
     split_set = data_set.split()
 ##This is where the error is occurring
     Counter = Counter(split_set)
     most_occuring = Counter.most_common(20)
     finalData.append(most_occuring)
     indexList += 1

print(finalData)

我不确定为什么它适用于 1 个元素,但是,您可以尝试更改变量的名称,因为Counter是对象可调用的名称。

还要在索引上添加一些"更好"的做法。

for idx, x in enumerate(docList):
    file = open(docList[idx], 'r')
    data_set = file.read().strip()
    file.close()
    split_set = data_set.split()
    CounterVariable = Counter(split_set)
    most_occuring = CounterVariable.most_common(20)
    finalData.append(most_occuring)

它适用于一个文档的原因是,最初引用collections.Counter类的 Counter 变量在循环的第一次迭代中分配了 Counter 实例后,不会用作构造函数。只有当循环处于第二次迭代中时,现在保存Counter实例的Counter变量才会通过调用它用作Counter构造函数,从而产生错误。

最新更新