简体   繁体   中英

Why do I get an IndexError?

I am trying to get trigrams out of a sentence and save them in a dictionary, with their frequenz as value. I wrote this:

trigrams = {}
sentence = ["What", "is", "happening", "right", "now"]

for word in sentence:
      if word != sentence[-1] or sentence[-2] and tuple((word, sentence[sentence.index(word) +1], sentence[sentence.index(word) +2])) not in trigrams:
             trigrams.update({tuple((word, sentence[sentence.index(word) +1], sentence[sentence.index(word) +2])):1})

Should look like this: ("what","is","happening"):1 ("is","happening","right"):1 etc

But now I am keep getting an IndexError in the update-line.

You can use lists as your tuples' contents are all of the same datatype (string)

It's probably easier to do:

trigrams = []
sentence = ["What", "is", "happening", "right", "now"]

for i in range(2,len(sentence)):
    trigrams.append([sentence[i-2],sentence[i-1],sentence[i]])

I guess if word != sentence[-1] or sentence[-2] is not what you want. Do you mean if word != sentence[-1] and word != sentence[-2] , meaning word does not equal either sentence[-1] nor sentence[-2] ?

Given you would like to keep your code structure with the tuple and change minimally your code, you can do this (not saying this might be a good approach for your problem, etc.):

trigrams = {}
sentence = ["What", "is", "happening", "right", "now"]

for index, word in enumerate(sentence):
    print index, word  # to understand how the iteration goes on
    if index < len(sentence)-2:
        if tuple((word, sentence[index+1], sentence[index+2])) not in trigrams:
            trigrams.update({tuple((word, sentence[index+1], sentence[index+2])):1})

You were getting an index error because you were accessing an element that didn't exist in tuple()... because the way you were doing the checking to see if were near the end of the list (the last two elements) wasn't done right.

The code you were using:

if word != sentence[-1] or sentence[-2]

is not right and you were comparing strings eventually and not the indexes, which is what is important here! Compare the indexes, not the values at those positions.

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