Oct-08-2016, 12:39 PM
Hello,
Here's a class for dynamically allocating a dictionary with an example
Here's a class for dynamically allocating a dictionary with an example
""" Dynamic nested dictionary Author: Larz60+ """ # import collections class DynamicNestedDict: def dynamic_nested_dict(self): return collections.defaultdict(self.dynamic_nested_dict) # Example of usage if __name__ == '__main__': keylist = ['bind', 'application level', 'binding'] loc = [20, 96, 100, 101, 102, 104, 105, 115, 193, 434, 546] dd = DynamicNestedDict() ddict = dd.dynamic_nested_dict() x = 'ddict' for n in range(len(keylist)): x = '{}[{}]'.format(x, repr(keylist[n])) x = x + ' = loc' exec(x) print("ddict['bind']['application level']['binding']: {}" .format(ddict['bind']['application level']['binding']))Any improvements appreciated