使用python3,我必须建立一个列表的字典的字典的字典。轻松构建一个词典或词典的词典
我不知道是否有更好的方法做下面的代码做(它看起来像垃圾代码......)
ret = {}
for r in listOfDictionnariesFromMongoDatabase:
a = TheObjectIUse(r)
sp = a.getSp()
ex = a.getEx()
th = a.getTh()
lo = a.getLo()
de = a.getDe()
if not sp in ret:
ret[sp] = {}
if not ex in ret[sp]:
ret[sp][ex] = {}
if not th in ret[sp][ex]:
ret[sp][ex][th] = {}
if not lo in ret[sp][ex][th]:
ret[sp][ex][th][lo] = []
ret[sp][ex][th][lo].append(de)
感谢您的帮助。
是的。 [collections.defaultdict](https://docs.python.org/2/library/collections.html#collections.defaultdict) – CoryKramer 2014-10-08 13:36:46
https://gist.github.com/hrldcpr/2012250 – 2014-10-08 13:55:32