TypeError: list indices must be integers, not str (boolean convertion actually) [duplicate]
Asked Answered
T

2

9
import nltk
import random
from nltk.corpus import movie_reviews

documents=[(list(movie_reviews.words(fileid)),category)
           for category in movie_reviews.categories()
           for fileid in movie_reviews.fileids(category)]

random.shuffle(documents)
#print(documents[1])

all_words=[]

for w in movie_reviews.words():
    all_words.append(w.lower())

all_words=nltk.FreqDist(all_words)

word_features = list(all_words.keys())[:3000]

def find_features(document):
    words = set(document)
    features=[]
    for w in word_features:
        features[w]= (w in words)

    return features

print((find_features(movie_reviews.words('neg/cv000_29416.txt'))))

featuresets = [(find_features(rev), category) for (rev,category) in documents]

After run, I am getting the error

features[w]= (w in words)
TypeError: list indices must be integers, not str

Please help me to solve it...

Thebes answered 3/8, 2016 at 8:10 Comment(4)
features is supposed to be a list or a dict ?Marxism
features defined as a list just above, should be a dict to accept strings as "indexes"Unmusical
Try declaring features as a dict features = {} instead of features = []Latoya
Obviously a common error: TypeError: list indices must be integers, not str Python, Python: TypeError: list indices must be integers, not str, TypeError: string indices must be integers, not str on Python Dictionary,...Gabrielegabriell
C
14

Only change that needs to be made is that features must be initialized to a dict ({}) rather than a list ([]) and then you could populate it's contents.

The TypeError was because word_features is a list of strings which you were trying to index using a list and lists can't have string indices.

features={}
for w in word_features:
    features[w] = (w in words)

Here, the elements present in word_features constitute the keys of dictionary, features holding boolean values, True based on whether the same element appears in words (which holds unique items due to calling of set()) and False for the vice-versa situation.

Carpospore answered 3/8, 2016 at 8:21 Comment(3)
You should really add why, the change is so minimalistic, that no one will see it, if not already aware of the error. So better add some descriptive text.Gabrielegabriell
@ Wolf: It's my bad. Was in a bit of haste and hence couldn't provide the needed explanation.Carpospore
You emphasize dict and list, but this is not automatically synonymous to {} and [] for users which are new to python...Gabrielegabriell
L
6

You have tried to index a list features with a string and it is not possible with python. List indices can only be integers. What you need is a dictionary.

Try using a defaultdict meaning that even if a key is not found in the dictionary, instead of a KeyError being thrown, a new entry is created

from collections import defaultdict

features = defaultdict()
for w in word_features:
    features[w] = [w in words]
Latoya answered 3/8, 2016 at 8:25 Comment(3)
You can also try -- why "also", what alternative are you referencing? The simplest solution? ;)Gabrielegabriell
The question was already answered before I could submit mine. So the word 'also'Latoya
Yes, but maybe the answer already given is poor (I really think that): throwing a piece of code and saying try this is not helpful, see my comment. I'd suggest to incorporate the obvious solution to the problem into your answer and say some more words about the mistake the OP made. Another alternative is to link to the answer (but I'm not really suggesting this here).Gabrielegabriell

© 2022 - 2024 — McMap. All rights reserved.