GeMattenDoG GeMattenDoG - 1 month ago 50
Python Question

Writing words in a list and their positions to a file

I have been assigned a task at school (I will copy paste the details as it will explain it better than I can)


Develop a program that identifies individual words in a sentence, stores these in a list and replaces each word in the original sentence with the position of that word in the list.


For example, the sentence

ASK NOT WHAT YOUR COUNTRY CAN DO FOR YOU ASK WHAT YOU CAN DO FOR YOUR COUNTRY


Contains the words ASK, NOT, WHAT, YOUR, COUNTRY, CAN, DO, FOR, YOU
The sentence can be recreated from the positions of these words in this list using the sequence

1,2,3,4,5,6,7,8,9,1,3,9,6,7,8,4,5


Save the list of words and the positions of these words in the sentence as separate files or as a single file.


Analyse the requirements for this system and design, develop, test and evaluate a program to:


  • identify the individual words in a sentence and store them in a list

  • create a list of positions for words in that list

  • save these lists as a single file or as separate files.




_

So far I have been able to write the words to a file but not the numbers -I am only getting the number 1- Any help is very welcome:)

words=['ASK','NOT','WHAT','YOUR','COUNTRY','CAN','DO','FOR','YOU','ASK','WHAT','YOU','CAN','DO','FOR','YOUR','COUNTRY']
word_raw = ['ASK','NOT','WHAT','YOUR','COUNTRY','CAN','DO','FOR','YOU']
numbers = ['']
if 'ASK' in words:
numbers.append('0')
numbers.append('9')

if 'NOT' in words:
numbers.append('1')

if 'WHAT' in words:
numbers.append('2')
numbers.append('10')

if 'YOUR' in words:
numbers.append('3')
numbers.append('15')

if 'COUNTRY' in words:
numbers.append('4')
numbers.append('16')

if 'CAN' in words:
numbers.append('5')
numbers.append('12')

if 'DO' in words:
numbers.append('6')
numbers.append('13')

if 'FOR' in words:
numbers.append('7')
numbers.append('15')
if 'YOU' in words:
numbers.append('8')
numbers.append('11')

print (numbers)
for x in range(len(words)):
MyFile = open('Task2File.txt', 'w')
with open("Task2File.txt", mode="w",encoding="utf-8") as my_file:
for words in word_raw:
my_file.write(words+"\n")
for x in range(len(numbers)):
MyFile = open('Task2FileNumbers.txt', 'w')
with open("Task2FileNumbers.txt", mode="w",encoding="utf-8") as my_file:
for numbers in numbers:
my_file.write(numbers+"\n")
print (numbers)

Answer

This is one of the things dictionaries are best at.

sentence = "ASK NOT WHAT YOUR COUNTRY CAN DO FOR YOU ASK WHAT YOU CAN DO FOR YOUR COUNTRY"
words = sentence.split()
word_num = 1
word_dict = {}
output = []
for word in words:
    if word not in word_dict:
        word_dict[word] = word_num
        word_num += 1
    output.append(word_dict[word])
print(output)

It's probably easiest to write the lists as a single file to make sure they are in the same order.

with open('output.txt', 'w+') as f:
     f.writelines(' '.join([key, str(value), '\n']) 
                  for key, value in sorted(word_dict.items(), key=lambda x: x[1]))