finnadie123
finnadie123

Reputation: 1

Python List : Index out of Range

I've been trying to create a program that has to read in a file, find the unique words and punctuation, put those to a list and then get the positions of each word and store them in a list. Then, using the lists the program will recreate the file. This is my code:

import time
import re
words = open('words.txt')
sentence = words.read()
uniquewords = []
positions = []
punctuation = re.findall(r"[\w']+|[.,!?;]", sentence)
for word in punctuation:
    if word not in uniquewords:
        uniquewords.append(word)
print("This file contains the words and punctuation ", uniquewords)
positions = [uniquewords.index(word) for word in punctuation]
recreated = " ".join([uniquewords[i] for i in positions])
print("In a list the text file words.txt can be shown as:")
print(positions)
print("Recreating sentence...")
print(recreated)

The program above does what it needs to, except it produces the following output:

This file contains the words and punctuation ['Ask', 'not', 'what', 'your', 'country', 'can', 'do', 'for', 'you', ',', '!']

In a list the text file words.txt can be shown as:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 2, 8, 5, 6, 7, 3, 4, 10]

Recreating sentence...

Ask not what your country can do for you , Ask what you can do for your country !

The positions list starts at 0, so as normal I tried just doing this:

positions = [uniquewords.index(word)+1 for word in punctuation]

However this produces the error

  File "C:\Users\Sam\Desktop\COMPUTING TEMP FOLDER\task 3.py", line 13, in <module>
    recreated = " ".join([uniquewords[i] for i in positions])
  File "C:\Users\Sam\Desktop\COMPUTING TEMP FOLDER\task 3.py", line 13, in <listcomp>
    recreated = " ".join([uniquewords[i] for i in positions])
IndexError: list index out of range

How can I make the list start at 1 without getting this error? Any help would be greatly appreciated.

Another small problem is that while the original string is

"Ask not what your country can do for you, Ask what you can do for your country!"

the actual output is instead

Ask not what your country can do for you , Ask what you can do for your country !

Upvotes: 0

Views: 322

Answers (2)

Dinesh Pundkar
Dinesh Pundkar

Reputation: 4196

Please check the below code. I changed bit for recreating string to solve space issue along with the indexing problem you were facing.

import time
import re
words = open("val.txt",'r')
sentence = words.readline()
uniquewords = []
positions = []
punctuation = re.findall(r"[\w']+|[.,!?;]", sentence)
for word in punctuation:
    if word not in uniquewords:
        uniquewords.append(word)
print("This file contains the words and punctuation ", uniquewords)
positions = [uniquewords.index(word)+1 for word in punctuation]
#recreated = " ".join([uniquewords[i-1] for i in positions])
recreated = ''
for i in positions:
     w = uniquewords[i-1]
     if w not in '.,!?;':
          w = ' ' + w
     recreated = (recreated + w).strip()

print("In a list the text file words.txt can be shown as:")
print(positions)
print("Recreating sentence...")
print(recreated)

Output:

    C:\Users\dinesh_pundkar\Desktop>python c.py
('This file contains the words and punctuation ', ['Ask', 'not', 'what', 'your',
 'country', 'can', 'do', 'for', 'you', ',', '!'])
In a list the text file words.txt can be shown as:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 3, 9, 6, 7, 8, 4, 5, 11]
Recreating sentence...
Ask not what your country can do for you, Ask what you can do for your country!

Upvotes: 0

StephenTG
StephenTG

Reputation: 2647

The problem is that you are incrementing every element of positions so that it displays as 1-indexed, then using that array when python is expecting 0-indexed. Try using:

recreated = " ".join([uniquewords[i-1] for i in positions])

instead

Upvotes: 1

Related Questions