Home > Software design >  How to solve "TypeError: list indices must be integers or slices, not str" with a list of
How to solve "TypeError: list indices must be integers or slices, not str" with a list of

Time:01-15

I have this list of words and their corresponding POS and other values:

sentence= [[{'entity': 'adj', 'score': 0.9004535, 'index': 1, 'word': 'we', 'start': 0, 'end': 7}], [{'entity': 'verb', 'score': 0.8782018, 'index': 1, 'word': 'have', 'start': 0, 'end': 6}], [{'entity': 'verb', 'score': 0.9984743, 'index': 1, 'word': 'become', 'start': 0, 'end': 3}], [{'entity': 'noun', 'score': 0.9953852, 'index': 1, 'word': 'see', 'start': 0, 'end': 6}]]

I'm trying to extract all words that are not "verbs" or "prep". on other words, I want to exclude verbs and prepositions. I used this code:

sentence = [ sub['word'] for sub in sentence if sub['entity']!='verb' ]

But I get this error:

TypeError: list indices must be integers or slices, not str

Thank you

CodePudding user response:

As stated in comments you are iterating over lists:

non_verb_words = [word[0]['word'] for word in sentence if word[0]['entity']!='verb']

CodePudding user response:

Sentence is a list of list: you can try with itirate in sentence[0] = sub in sentence[0]:

sentence = [ sub['word'] for sub in sentence[0] if sub['entity']!='verb' ]
  • Related