[英]How to declare a nested dictionary in Python?
I want to declare a nested dictionary, with the key value pairs being a string and dict, with this nested dict having a key value pair of string and int).我想声明一个嵌套字典,键值对是一个字符串和一个字典,这个嵌套的字典有一个字符串和 int 的键值对)。 How would I go about doing this?
我该怎么做呢? Thanks!
谢谢!
The same way as you make a regular dictionary, except put it inside another dictionary.与制作普通词典的方法相同,只是将其放入另一本词典。
outer_dict = {
"inner1": {
"keyA": 1,
"keyB": 2,
"keyC": 3,
},
"inner2": {
"keyD": 4,
"keyE": 5,
"keyF": 6,
"keyG": 7,
},
}
If you wanted to annotate this with type hinting, you would do essentially the same thing, putting a Dict
inside another Dict
:如果你想用类型提示来注释它,你基本上会做同样的事情,把一个
Dict
放在另一个Dict
:
outer_dict: Dict[str, Dict[str, int]] = { ... }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.