python中的嵌套
嵌套:将一系列字典存储在列表中,或将列表作为值存储在字典中,这称为嵌套。既可以在列表中嵌套字典,也可以在字典中嵌套列表,甚至在字典中嵌套字典。
一、列表中嵌套字典
1)一般创建方式:
student_A ={"name":"Allen","age":"14","grade":"8"} student_B ={"name":"Jack","age":"12","grade":"6"} student_C ={"name":"Lucy","age":"13","grade":"7"} student_D ={"name":"polo","age":"14","grade":"8"} #上述四行,创建了4个字典,每个字典代表一个学生 students=[student_A,student_B,student_C,student_D]#将上述4个学生放名为students的列表中 for student in students: #遍历students列表 print(student) #打印每个学生 print(" ")