Nested defaultdict of defaultdict

前端 未结 8 2407
谎友^
谎友^ 2020-11-22 12:19

Is there a way to make a defaultdict also be the default for the defaultdict? (i.e. infinite-level recursive defaultdict?)

I want to be able to do:

x         


        
8条回答
  •  迷失自我
    2020-11-22 12:41

    There is a nifty trick for doing that:

    tree = lambda: defaultdict(tree)
    

    Then you can create your x with x = tree().

提交回复
热议问题