简体   繁体   English

Defaultdict 不返回默认值

[英]Defaultdict not returning default values

d1 = defaultdict(lambda : defaultdict(lambda : defaultdict (lambda : 0)))
d1['first']['second'] = 2 #Assigning some value
d1['first']['third']   #Expecting to return the default value, which is 0, but ...
defaultdict(<function <lambda>.<locals>.<lambda>.<locals>.<lambda> at 0x7ff5765421e0>, {})

Why is this not returning 0?为什么这不返回0?

Because there are three levels of defaultdict s until you reach the value 0 , so you need to do:因为在达到值0之前存在三个级别的defaultdict ,所以你需要这样做:

>>> d1['first']['second']['third']
0

You have 3 levels of defaultdict .您有 3 个级别的defaultdict So to get the default value you need 3 indexes.因此,要获得默认值,您需要 3 个索引。

print(d1[1][2][3])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM