defaultdictはkeyが存在しなかったときに、そのキーでentryを自動生成してdefault valueとしてdefaultdictに渡された引数の型の値をつくる
http://stackoverflow.com/questions/5029934/python-defaultdict-of-defaultdict
defaultdict(defaultdict(int)) in order to make the following code work?
for x in stuff: d[x.a][x.b] += x.c_int d needs to be built ad-hoc, depending on x.a and x.b elements. I could use: for x in stuff: d[x.a,x.b] += x.c_int but then I wouldn't be able to use: d.keys() d[x.a].keys()
これでできます。
defaultdict(lambda : defaultdict(int))
上の式を説明してくれてます。
Yes sure, the argument of a defaultdict (in this case is lambda : defaultdict(int)) will be called when you try to access a key that don't exist and the return value of it will be set as the new value of this key which mean in our case the value of d[Key_dont_exist] will be defaultdict(int), and if you try to access a key from this last defaultdict i.e. d[Key_dont_exist][Key_dont_exist] it will return 0 which is the return value of the argument of the last defaultdict i.e. int(), Hope this was helpful.