嵌套
列表字典
#列表内嵌套字典
player_0={'name':'jordon','favouritefood':'apple'}
player_1={'name':'kd','favouritefood':'pear'}
player_2={'name':'curry','favouritefood':'banana'}
player_3={'name':'andrew_wiggins','favouritefood':'watermelon'}
players=[player_0,player_1,player_2,player_3]
for player in players:
print(player)
效果:
字典列表
#字典中嵌套列表
superstars_favourite_foods={
'jordon':['apple','pear'],
'stephen':['watermalon'],
'kd':['melon'],
}
for superstar_name,superstars_favourite_food in superstars_favourite_foods.items():
print(f"{superstar_name}'s favourite food is :")
for food in superstars_favourite_food:
print(food)
效果:
字典嵌套字典
#字典内嵌套字典
superstars={
'stephen':{
'first_name':'curry',
'last_name':'stephen',
'team':'warrior',
},
'kd':{
'first_name':'durant',
'last_name':'kevin',
'team':'sum',
},
}
for superstar_name,superstar_info in superstars.items():
print(f"{superstar_name}'s full name is {superstar_info['first_name']} {superstar_info['last_name']}")
print(f"{superstar_name}'s team name is {superstar_info['team']}")
效果: