反転するのがおそらくより良い方法です:
In [17]: d = {table[k1][k2]: (k1,k2) for k1 in table for k2 in table[k1]}
これは、任意の深さと「不規則な」dictを処理するソリューションです。
def invert_arbitrary(d, ldict, p=[]):
for k, v in ldict.items():
if isinstance(v, dict):
invert_arbitrary(d, v, p + [k])
else:
d[v] = p + [k]
例:
table = {'x1': {'y1': 1, 'y2': 2},
'x2': {'y1': 3,
'y2': {'z1': 4, 'z2': 5}},
'x3': 6}
In [40]: d = dict()
In [41]: invert_arbitrary(d, table)
In [42]: d
Out[42]:
{1: ['x1', 'y1'],
2: ['x1', 'y2'],
3: ['x2', 'y1'],
4: ['x2', 'y2', 'z1'],
5: ['x2', 'y2', 'z2'],
6: ['x3']}