Python: build dictionary from list -
for assignment have build dictionary keys elements list , values list positions each element, example:
x = [13, 15, 27, 11, 13, 27] > dict = {13:[0,4], 15:[1], 27:[2,5], 11:[3]} i can't positions repeated element in list.
from collections import defaultdict d = defaultdict(list) i,j in enumerate(x): d[j].append(i) using defaultdict default being empty list , enumerate provide index
Comments
Post a Comment