arrays - Python "list index out of range" error -


my goals have list of lists, each item in outer list contains word in it's first index, , number of times has come across in second index. example, should this:

[["test1",0],["test2",4],["test3",8]] 

the issue when try to, instance, access word "test1" first inner-list, index out of range error. here code how attempting this:

stemmedlist = [[]]  f = open(a_document_name, 'r')  #read each line of file filelines = f.readlines() fileline in filelines:     #here end stoplist, list of words     thisreview = hw1.read_line(fileline)['text']     tokenlist = hw1.tokenize(thisreview)     stoplist = hw1.stopword(tokenlist)      #for each word in stoplist, compare terms in return list     #see if exists, if add 1 second parameter, else     #add list ["word", 0]     word in stoplist:         #if list not empty         if not len(unstemmedlist) == 1:   #for reason have see if list empty, i'm assuming when it's empty returns length of 1 since i'm initializing list of lists??             print "list not empty."             innerlist in unstemmedlist:                 if innerlist[0] == word:                     print "adding 1 [" + word + ", " + str(innerlist[1]) + "]"                     innerlist[1] = (innerlist[1] + 1)                 else:                     print "adding [" + word + ", 0]"                     unstemmedlist.append([word, 0])         else:             print "list empty."             unstemmedlist.append([word, 0])             print unstemmedlist[len(unstemmedlist)-1]  return stemmedlist 

the final output ends being:

list empty. ["test1",0] list not empty"

crash list index out of range error points line if innerlist[0] == word

you have a = [[]]

now, when appending list after encountering first word, have

a = [ [], ['test', 0] ]

in next iteration accessing, 0th element of empty list doesn't exist.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

django - CSRF verification failed. Request aborted. CSRF cookie not set -