简体   繁体   中英

Keep getting TypeError: 'list' object is not callable

I'm not sure where my error is but here's the code where i got the error from index = plottest(doc) :

for doc in plottest:

    for word in wordsunique:

        if word in doc:
            word = str(word)
            index = plottest(doc)
            positions = list(np.where(np.array(index) == word)[0])
            idfs = tfidf(word,doc,plottest)

            try:
                worddic[word].append([index, positions, idfs])
            except:
                worddic[word] = []
                worddic[word].append([index, positions, idfs])

As said by @Robin Zigmond in a comment, you are trying to call a list, as you would call a function, using the (...) syntax. The following:

>>> def f(x): return 2*x
... 
>>> f(2)
4

Is different from:

>>> L=[1,2,3]
>>> L(2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'list' object is not callable

The latter doesn't work because [1,2,3] is not callable. The Python documentation enumerates callable types:

  • User-defined functions
  • Instance methods
  • Generator functions
  • Coroutine functions
  • Asynchronous generator functions
  • Built-in functions
  • Built-in methods
  • Classes
  • Class Instances: Instances of arbitrary classes can be made callable by defining a __call__() method in their class.

A list (ie a list instance) is none of them, since the list class does not have a __call__() method :

>>> dir(list)
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

In your example, the first lines states that plottest is an iterable. The error shows it is a list. And you try to call it with index = plottest(doc) . My guess is that you want to get the index of doc in plottest . To achieve this in Python, you can write:

for index, doc in enumerate(plottest):
    ...

Hope it helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM