Python how to create a dict of dict of list with defaultdict

后端 未结 3 1735
再見小時候
再見小時候 2021-01-18 13:20

How do I create a dict of dict of lists using defaultdict? I am getting the following error.

>>> from collections import defaultdict
>>> a=         


        
3条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-18 13:24

    You may have to do like this.

    >>> from collections import defaultdict
    >>> a=defaultdict()
    >>> a["testkey"]=None
    >>> a["testkey"]=defaultdict(list)
    >>> a["testkey"]["list"]=["a","b","c"]
    >>> a
    defaultdict(None, {'testkey': defaultdict(, {'list': ['a', 'b', 'c']})})
    

提交回复
热议问题