how to change all the words in a sentence into a number, and also be able to change it back
<p>OK, so I have to make a program in python, that is able to make a number for each word in a sentence, and if the word is the same it will have the same number, but I have to be able to change it back to the original sentence as well.</p>

<p>For example, "today the cat sat on the mat" would be changed into 0 1 2 3 4 1 5</p>

<p>0 being "today" and 5 being "mat", and 1 being "the"</p>

<p>so basically if I get the idea of this its creating a variable for each word being a number, the only problem is I have no idea were to start making this program, </p>

<p>some help would really be appreciated thanks :)</p>

<p>This sounds a lot like a school assignment. Since from my experience I can say that the best practice is to do it yourself, I'd suggest only looking at the hints I give first, and if you're really stuck, look at the code.</p>

<p>Hint 1: </p>

<blockquote class="spoiler">
  <p> Separate the sentence into a list of words first.</p>
</blockquote>

<p>You can do this using</p>

<blockquote class="spoiler">
  <p> <code>words = sentence.split(" ")</code></p>
</blockquote>

<p>Hint 2:</p>

<blockquote class="spoiler">
  <p> You want to create some kind of mapping from word to number, but also in reverse.</p>
</blockquote>

<p>You can do this using</p>

<blockquote class="spoiler">
  <p> dicts. Treat them as literal dictionaries: Make one dict with the words as keys and the numbers as values, and one with numbers as keys and words as values. That'll allow you to look up numbers and words as necessary. Note that the dict which has numbers as keys could theoretically be a list, however this might break when you don't want numbers anymore, or when you want to delete certain entries.</p>
</blockquote>

<p>Hint 3:</p>

<blockquote class="spoiler">
  <p> You'll need to generate an entry in both dicts mentioned in Hint 2 for every word - to make sure you can go back. Thus, a for loop over the list with words, and at every iteration generate an entry in both dicts.</p>
</blockquote>

<p>Hint 4:</p>

<blockquote class="spoiler">
  <p> In order to make sure same words map to the same number, there are two ways to go. Firstly, during the iteration, you can simply check if the word is already in the words->numbers dict. If it is, skip it. Otherwise, generate the entries. Note that you need to keep track of the highest number you have used. Secondly, you could convert the list with words to a set. This will directly remove all duplicates, though you may lose the ordering of the words, meaning that in your example it might become 3 2 0 5 4 2 1</p>
</blockquote>

<p>I hope this will be useful. If absolutely necessary, I could provide the code to do it, but I highly recommend figuring it out yourself.</p>

python python-3.x variables text sentence