I am currently writing a program that will take in a text file, and then count the frequency of each word in the file, after lower casing every word and stripping its punctuation.
Here is my code:
import sys
import string
incoming =[]
freq =[]
word =[]
count = 0
index = 0
i = 0
with open(sys.argv[1], "r") as word_list:
for line in word_list:
#word is the string of the .txt file
#strips punctuation and lower cases each word
for words in line.split():
words = words.translate(string.maketrans("",""), string.punctuation)
words = words.lower()
incoming.append(words)
#incoming is now an array with each element as a word from the file
for i in range(len(incoming)-1):
if (incoming[i]) not in word:
#WORD[i] = word[index]
word[index] = incoming[i]
freq[index] = 1
index += 1
else:
freq[index] = freq[index] + 1
for j in word:
print "%s %d", word[j], freq[j]
I am getting the error:
File "wordfreq.py", line 26, in <module>
word[index] = incoming[i]
IndexError: list assignment index out of range
But I fail to see how it can be out of range. Neither index
nor i
go out of range as far as I can tell. I am new to Python and am having a lot of trouble with the 'for' loop syntax. Any tips would be much appreciated.