I have a list say l = [10,10,20,15,10,20]
. I want to assign each unique value a certain \"index\" to get [1,1,2,3,1,2]
.
This is my code:
For completness, you can also do it eagerly:
from itertools import count wordid = dict(zip(set(list_), count(1)))
This uses a set to obtain the unique words in
list_
, pairs each of those unique words with the next value fromcount()
(which counts upwards), and constructs a dictionary from the results.
Original answer, written by nneonneo.