#!/usr/bin/python
#Sort by key example for dictionary of dictionary.
#In order to sort by keys, you have to remember that
#the dictionary is immutable whereas a list is mutable.
#
#In list, sort() is available whereas it is not available
#in dictionary.
#
#Need to loop through the sorted list and substitute the
#key to get the value by using dict.get()
def cmpTwoNums(a,b):
print "a: %d, b: %d" % (a,b)
if int(a) <= int(b):
#print "asmall: %d" % a
return 1
else:
#print "bsmall: %d" % b
return -1
d1 = {'a':1, 'b':20, 'c':30}
d2 = {'c':10, 'd':2, 'e':40}
d3 = {'f':10, 'g':100, 'h':1000}
d4 = {'c':4, 'n':3, 'm':10}
totalDict = {}
superDict = {'dict1':d1,'dict2':d2,'dict3':d3,'dict4':d4}
for k,v in superDict.items():
for k1,v1 in v.items():
if totalDict.has_key(v1):
totalDict[v1].append(k1)
else:
totalDict[v1] = [k1]
keys = totalDict.keys()
keys.sort(cmpTwoNums)
#print keys
for k in keys:
print "key: %d, value: %s" % (k, totalDict.get(k))