You want to create some kind of mapping from word to number, but also in reverse.
你可以使用
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.
提示3:
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.
提示4:
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
这听起来很像学校的作业。因为根据我的经验,我可以说最好的做法是自己动手,所以我建议只看我给出的提示,如果你真的卡住了,看看代码。你知道吗
提示1:
你可以使用
提示2:
你可以使用
提示3:
提示4:
我希望这会有用。如果绝对必要的话,我可以提供代码来完成它,但是我强烈建议你自己去弄清楚。你知道吗
相关问题 更多 >
编程相关推荐