I have a dictionary in the following form
dict = {
"a" : {"a1" : 1},
"b" : {"a2" : 1, "a3" : 2},
"c" : {"a2" : 3, "a4" : 3}
}
and I need the reverse index dictionary, in this form:
inverseDict = {
"a1" : {"a" : 1},
"a2" : {"b" : 1, "c" : 3},
"a3" : {"b" : 2},
"a4" : {"c" : 3}
}
Basically
inverseDict = {dict.value.key : { dict.key : dict.value.value}}
So essentially, I need the keys of the values as keys, and the keys as keys of values, while at the same time joining results for duplicate new keys etc.
I've tried to do
ks = dict.keys()
vals = dict.values()
ks2 = vals.keys()
vals2 = vals.values()
if this makes any sense
But I'm getting an error
'dict_values' object has no attribute 'keys'
Which from what I understand is because dict.values() .keys() .items() return "views" instead of the actual element itself, but I don't know hot to go about fixing this problem.
Also is there a more efficient solution I should consider, because my actual dict is pretty large (~10k keys), and the resulting inverse dict will also be large ( >3k keys)