Declaring a multi dimensional dictionary in python

后端 未结 8 1624
闹比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:51

    u can try this, it is even easier if it is string

    new_dic = {}
    a = 1
    new_dic[a] = {}
    b = 2
    new_dic[a][b] = {}
    c = 5
    new_dic[a][b]={c}
    

    type

    new_dic[a][b]
    >>>'5'
    

    For string

    new_dic = {}
    a = "cat"
    new_dic[a] = {}
    b = "dog"
    new_dic[a][b] = {}
    c = 5
    new_dic[a][b] = {c}
    

    type

    new_dic["cat"]["dog"]
    >>>'5'
    

提交回复
热议问题