Reputation: 453
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!
Upvotes: 1
Views: 2578
Reputation: 7799
A possible solution:
dictionary= {}
for i, x in enumerate(wordlist):
if not x in dictionary : dictionary[x]= []
dictionary[x].append( i )
Upvotes: 0
Reputation: 11704
>>> wl = ['I', 'am', 'going', 'to', 'go', 'to', 'town']
>>> {w: [i for i, x in enumerate(wl) if x == w] for w in wl}
{'town': [6], 'I': [0], 'am': [1], 'to': [3, 5], 'going': [2], 'go': [4]}
Upvotes: 2
Reputation: 7799
import collections
dictionary= collections.defaultdict(list)
for i, x in enumerate( wordlist ) :
dictionary[x].append( i )
Upvotes: 0
Reputation: 98516
You can do this way:
dictionary = {}
for i, x in enumerate(wordlist):
dictionary.setdefault(x, []).append(i)
Explanation:
index()
. It is more efficient and cooler to use enumerate()
.dict.setdefault()
uses the first argument as key. If it is not found, inserts the second argument, else it ignores it. Then it returns the (possibly newly inserted) value.list.append()
appends the item to the list.You will get something like this:
{'I': [0], 'am' : [1], 'going' : [2], 'to': [3, 5], 'go' : [4], 'town' : [6]}
With lists instead of tuples, and using lists even if it is only one element. I really think it is better this way.
UPDATE:
Inspired shamelessly by the comment by @millimoose to the OP (thanks!), this code is nicer and faster, because it does not build a lot of []
that are never inserted in the dictionary:
import collections
dictionary = collections.defaultdict(list)
for i, x in enumerate(wordlist):
dictionary[x].append(i)
Upvotes: 7
Reputation: 799430
Objects are objects, regardless of where they are.
dictionary[x] = []
...
dictionary[x].append(y)
Upvotes: 0