Declaring a multi dimensional dictionary in python

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

    One simple way is to just use tuples as keys to a regular dictionary. So your example becomes this:

    new_dic[(1, 2)] = 5

    The downside is that all usages have to be with this slightly awkward convention, but if that's OK, this is all you need.

提交回复
热议问题