Appending to a list inside of a dictionary
I'm going through a list of individual words and creating a dictionary
where the word is the key, and the index of the word is the value.
dictionary = {}
for x in wordlist:
dictionary[x] = wordlist.index(x)
This works fine at the moment, but I want more indexes to be added for
when the same word is found a second, or third time etc. So if the phrase
was "I am going to go to town", I would be looking to create a dictionary
like this:
{'I': 0, 'am' : 1, 'going' : 2, 'to': (3, 5), 'go' : 4, 'town' : 6}
So I suppose I need lists inside the dictionary? And then to append more
indexes to them? Any advice on how to accomplish this would be great!
No comments:
Post a Comment