I have a text file named corpus.txt containing the following 4 lines of text
peter piper picked a peck of pickled peppers
a peck of pickled peppers peter piper picked
if peter piper picked a peck of pickled peppers
where s the peck of pickled peppers peter piper picked
I want the output of the program to print a word and the number of times it occurs for example like
4 peter
4 piper
etc.
This is the code that I have written
f = open("corpus.txt","r")
w, h = 100, 100;
k=1
a=0
uwordcount=[]
for i in range(100):
uwordcount.append(0)
uword = [[0 for x in range(w)] for y in range(h)]
l = [[0 for x in range(w)] for y in range(h)]
l[1] = f.readline()
l[2] = f.readline()
l[3] = f.readline()
l[4] = f.readline()
lwords = [[0 for x in range(w)] for y in range(h)]
lwords[1]=l[1].split()
lwords[2]=l[2].split()
lwords[3]=l[3].split()
lwords[4]=l[4].split()
for i in [1,2,3,4]:
for j in range(len(lwords[i])):
uword[k]=lwords[i][j]
uwordcount[k]=0
for x in [1,2,3,4]:
for y in range(len(lwords[i])):
if uword[k] == lwords[x][y]:
uwordcount[k]=uwordcount[k]+1
for z in range(k):
if uword[k]==uword[z]:
a=1
if a==0:
print(uwordcount[k],' ',uword[k])
k=k+1
I am getting the error
Traceback (most recent call last): File "F:\New folder\1.py", line 25, in if uword[k] == lwords[x][y]: IndexError: list index out of range
Can anyone tell me what is the problem here