Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: list indices must be integers, not str (boolean convertion actually)

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...

like image 378
RokiDGupta Avatar asked Aug 03 '16 08:08

RokiDGupta


People also ask

How do you fix TypeError list indices must be integers or slices not str?

The Python "TypeError: list indices must be integers or slices, not str" occurs when we use a string instead of an integer to access a list at a specific index. To solve the error, use the int() class to convert the string to an integer, e.g. my_list[int(my_str)] .

How do I fix TypeError string indices must be integers?

The Python "TypeError: string indices must be integers" occurs when we use a non-integer value to access a string at an index. To solve the error, make sure to use an integer, e.g. my_str[2] or a slice, e.g. my_str[0:3] when accessing a string at a specific index.

How do I fix list indices must be integers or slices not tuple in Python?

The Python "TypeError: list indices must be integers or slices, not tuple" occurs when we pass a tuple between the square brackets when accessing a list at index. To solve the error, make sure to separate nested list elements with commas and correct the index accessor.

What is TypeError string indices must be integers in python?

The typeerror: string indices must be integers indicates that we are attempting to access a value from an iterable using a string index rather than an integer index. The iterable objects are indexed using numbers. Therefore, an error will be returned when you try to obtain an iterable object using a string value.


1 Answers

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.

like image 69
Nickil Maveli Avatar answered Oct 25 '22 19:10

Nickil Maveli