“Python/gloss python nested dictionaries”的版本间差异
来自菜鸟教程
小 (Pywikibot 4.4.0.dev0) |
小 (机器人:添加分类Python基础教程) |
||
第89行: | 第89行: | ||
</div> | </div> | ||
<br /> | <br /> | ||
+ | |||
+ | [[分类:Python基础教程]] |
2020年10月29日 (四) 08:48的最新版本
<languages />
Python嵌套字典
嵌套词典
词典也可以包含许多词典,这被称为嵌套词典。
例
创建一个包含三个词典的字典:
myfamily = { "child1" : { "name" : "Emil", "year" : 2004 }, "child2" : { "name" : "Tobias", "year" : 2007 }, "child3" : { "name" : "Linus", "year" : 2011 } }
或者,如果您想嵌套三个已经作为字典存在的字典:
例
创建三个字典,然后创建一个包含其他三个字典的字典:
child1 = { "name" : "Emil", "year" : 2004 } child2 = { "name" : "Tobias", "year" : 2007 } child3 = { "name" : "Linus", "year" : 2011 } myfamily = { "child1" : child1, "child2" : child2, "child3" : child3 }