Declaring a multi dimensional dictionary in python

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

    Here is a dictionary that contains another dictionary as the value for key 1:

    >>> new_dic = {}
    >>> new_dic[1] = {2:5}
    >>> new_dic
    {1: {2: 5}}
    

    The problem that you had with

    new_dic={}
    new_dic[1][2]=5
    

    is that new_dic[1] does not exist, so you can't add a dictionary (or anything for that matter) to it.

提交回复
热议问题