Search code examples
pythonpython-3.xnltktextblob

Why I'm unable to pass list to word_counts in textblob?


As shown in the quickstart.

I have a list of words to be searched programmatically, but .word_counts['ekki'] (as shown in the quickstart), is giving me an error.

>>> import textblob
>>> str = textblob.TextBlob("hello im programmer")
>>> lis = ["hi","hello"]
>>> str.word_counts[i for i in lis]
  File "<stdin>", line 1
    str.word_counts[i for i in lis]
                        ^
SyntaxError: invalid syntax

Code snippets or helpful links appreciated.


Solution

  • You need to replace this;

    str.word_counts[i for i in lis]
    

    With this;

    for i in lis:
        print(str.word_counts[i])
    

    Your original line doesn't make any sense since you're trying to use a list comprehension as an index to acces a list.