- 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.
How would i save my output to a separate file?
How would i save my output to a separate file?
file=open("filename.txt","w")
file.write(*map(position.__getitem__, words)) #Your last line
file.close()
By the way, don't share your code through an image and use markdown code formatting like above.
I recognise the question you are trying to answer from GSCE Computing (UK).
Aside from your request as to how to store the words in a file, you use a confusing way to save the position of the words.
Using position[word] = len(position) + 1
is saving the position of the word in the list of words, not the position in the original sentence. In fact, the number stored against the word
in position
is not used, since you map the word against the words
list to get the position.
Writing to a file is not difficult, do a search of Stackoverflow.