python - Comparing dict values and popping the keys -
i have big dictionary simplified looks this:
my_dict = {'a': [-33.27, -2.12, 5.23], 'b': [-57.11, 9.82, -26.13], ...}
so keys strings , values lists of floats.
what want reduce it's size running criterion finds , removes redundant key: value pairs.
the criterion in pseudo-code is:
for every key i, find whether different key j exists in dict such that:
value_of_key_i[0] > value_of_key_j[0] and
value_of_key_i[1] > value_of_key_j[1] and
abs(value_of_key_i[2]) < abs(value_of_key_j[2])
what wrote task this:
to_remove = [] ilcs, iloads in running_load.items(): jlcs, jloads in running_load.items(): if iloads[0] > jloads[0] , iloads[1] > jloads[1] , abs(iloads[2]) < abs(jloads[2]): # print(iloads, jloads) to_remove.append(ilcs) break in to_remove: running_load.pop(i)
which work, explicitly traversing dict twice , necessary additional for-loop popping feels inefficient..
is there better way? more efficient generators and, let's say, any()
?
p.s: problem approach cannot test equality since @ point values going tested against (and yes 1 check , continue
but...)
instead of using list of elements remove, create new dictionary , add in elements kept.
i don't see way reduce o(n^2) complexity in initial phase (compare every element against every other element), mentioned above sorting some.
also, think generate 2 different lists identical content items in dictionary (one list in each for). shouldn't count compared "compare every item every other item", still helps.
Comments
Post a Comment