Declaring a multi dimensional dictionary in python

后端 未结 8 1631
闹比i
闹比i 2020-12-08 02:03

I need to make a two dimensional dictionary in python. e.g. new_dic[1][2] = 5

When I make new_dic = {}, and try to insert values, I get a <

8条回答
  •  广开言路
    2020-12-08 02:37

    Simply, you can use defaultdict

    from collections import defaultdict
    new_dic = defaultdict(dict)
    new_dic[1][2]=5
    >>>new_dic
    defaultdict(, {1: {2: 5}})
    

提交回复
热议问题