dictionary - Optimizing something in Python -


so, have list of dicts trying move these values on sparse matrix:

matrix = [[0]*large in xrange(small)] #large 150k , small 10k 

so, values this, have dict of unique keys, length of equal large, , determines values mapped keys in individual dicts go based on index, so:

for in range(len(lst)): dic = lst[i] key in dic.keys():     vectors[i][ordering.index(key)] = dic.get(key, 0) 

but taking long time do. like.... forever. (ordering dict merged copy of dicts... want data sparse matrix, not dict using knowing index of keys should use sparse matrix)

ordering cause.

based on way use it, ordering list, ordering.index(key) has full scan of list find index.

change dictionary, intended index value, this:

ordering_dict = {} in range(len(ordering)):     ordering_dict[ordering[i]] = 

and change assignment to:

vectors[i][ordering[key]] = dic.get(key, 0) 

this remove equivalent of entire iteration of len(ordering) each assignment, means len(lst)*len(dic)*len(ordering) fewer total operations.


Comments