Can you suggest me a dictionary comprehension to iterate over list of items and hash them in dictionary with value equal to auto-incremented integer id? Or some method to get value from generator or other list?
Is there possible something like this:
iverted_index_dict = {key: (auto_increment_here) in object_list }
my current sollution:
object_list = ["trip", "thre", "tree", "noya", "goya", "voya"]
invid1 = {object_list[i]: i+1 for i in xrange(0,len(object_list))}
print invid1
invid2 = {}
i=0
for item in object_list:
i += 1
invid2[item] = i
print invid2
You can use enumerate
:
>>> {k:v for v, k in enumerate(object_list, 1)}
{'goya': 5, 'noya': 4, 'thre': 2, 'tree': 3, 'trip': 1, 'voya': 6}