-1

My goals is to have a list of lists, where each item in the outer list contains a word in it's first index, and the number of times it has come across it in the second index. As an example, it should look like this:

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

The only issue is that when I try to, for instance, access the word "test1" from the first inner-list, I get an index out of range error. Here is my code for how I am attempting to do this:

stemmedList = [[]]

f = open(a_document_name, 'r')

#read each line of file
fileLines = f.readlines()
for fileLine in fileLines:
    #here we end up with stopList, a list of words
    thisReview = Hw1.read_line(fileLine)['text']
    tokenList = Hw1.tokenize(thisReview)
    stopList = Hw1.stopword(tokenList)

    #for each word in stoplist, compare to all terms in return list to
    #see if it exists, if it does add one to its second parameter, else
    #add it to the list as ["word", 0]
    for word in stopList:
        #if list not empty
        if not len(unStemmedList) == 1:   #for some reason I have to do this to see if list is empty, I'm assuming when it's empty it returns a length of 1 since I'm initializing it as a list of lists??
            print "List not empty."
            for innerList in unStemmedList:
                if innerList[0] == word:
                    print "Adding 1 to [" + 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 up being:

List is empty. ["test1",0] List not empty"

Crash with list index out of range error which points to the line if innerList[0] == word

2
  • 1
    why not use a Counter? This is exactly what its for.
    – cmd
    Sep 9, 2014 at 21:52
  • Your else: print "List empty." statement is inside the loop. Did you mean for that line to be called there?
    – Celeo
    Sep 9, 2014 at 21:54

3 Answers 3

0

You have a = [[]]

Now, when you are appending to this list after encountering first word, you have

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

In the next iteration you are accessing the 0th element of an empty list which doesn't exist.

0

Assuming that stemmedList and unStemmedList are similar

stemmedList = [[]]

you have an empty list in your list of lists, it has no [0]. Instead just initialize it to:

stemmedList = []
0

Isn't this simpler?

counts = dict()
def plus1(key):
    if key in counts:
        counts[key] += 1
    else:
        counts[key] = 1

stoplist = "t1 t2 t1 t3 t1 t1 t2".split()
for word in stoplist:
    plus1(word)

counts
{'t2': 2, 't3': 1, 't1': 4}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.