0
  • 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?

This is my code

Tony
  • 9,672
  • 3
  • 47
  • 75
Nathan
  • 17
  • 1
  • https://stackoverflow.com/questions/9536714/python-save-to-file – GeckStar Nov 04 '16 at 00:02
  • 1
    Just a note, very obvious comments like "this sets " are unnecessary and distract from the important comments. Where the code can speak for itself, it's best to let it do so. – BallpointBen Nov 04 '16 at 00:21
  • Please [edit] your code into the body of your question. It takes jut a cople seconds to ctrl-C ctrl-V it in, and saves anyone helping you the time of re-writing it themselves. You'll get much better answers if it's easier for the people helping you. – Knells Nov 04 '16 at 00:48

2 Answers2

0
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.

atakanyenel
  • 1,367
  • 1
  • 15
  • 20
0

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.

Community
  • 1
  • 1
Tony
  • 9,672
  • 3
  • 47
  • 75
  • I'm a developer, a friend of mine teaches computer science and he asked me about this question. It's really not too hard if you search (google) for information about writing a reading to files. As a bit of a hint, you could also think about storing the positions of the words in a string, rather than a list, and write that "word" to a file. Of course, that only works while the position values are less than 10. – Tony Nov 04 '16 at 01:46