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
}
亲自试一试 »