Recreating a sentence using a text file with unique words and a text file with a set of position numbers

267 views Asked by At

I am trying to read a set of unique words from one text file and a set of positions of a sentence from another text file. I am them using the data from these text files to recreate the sentence. However I am getting the following error: 'list' object is not callable for following line of code: recreated = ' '.join(Words1(pos) for pos in Numbers1).

import re

#opens the file to be able to read
wordsfile = open("Individual Words.txt", "r")

#splits the file into individual words
filewords = wordsfile.read().split()
print ("These are the values stored within the Individual Words file")
print(filewords)
print ("")

#opens the file ready to read
positionsfile = open("Individual Positions.txt", "r")
filepositions = positionsfile.read()

#removes square brackets and commas from the file
positions = re.sub("[\.\[\]\\,\:;(\)\.\!\$\£\&\?\'\"\&\(\)]", "",       filepositions, 0, 0)

#splits the numbers up into individual instances
positions = positions.split()

#converts the instances from strings to integers
positions = [int(x) for x in positions]
print("These are the positions from the Individual Positions file")
print (positions)
print ("")

Words1=[]
Numbers1=[]

x=0
for position in range(len(filewords)):
    word=filewords[x]
    if word not in Words1:
        Words1.append(word)
    x=x+1

y=0

for position in range(len(filepositions)):
    word=filepositions[y]
    if word not in Numbers1:
        Numbers1.append(word)
    y=y+1

recreated = ' '.join(Words1(pos) for pos in Numbers1)

print (recreated)
0

There are 0 answers