1、列表中嵌套字典
a1 = {'color':'green', 'points':5}
a2 = {'color':'yello', 'points':25}
a3 = {'color':'red', 'points':15}
ass = [a1, a2, a3]
for a in ass:
print(a)
输出结果:
2、字典中添加列表
pizza={
'curst':'thick',
'toppings': ['mushroom','extra cheese']
}
print("Your order: " + pizza['curst'] + "-pizza" +
"with the following toppings:")
for topping in pizza['toppings']:
print(' '+topping)
示例结果:
3、在字典中存储字典
users = {
'ast1':{
'first':'albert',
'last':'en',
'location':'henan'
},
'ast2':{
'first':'marry',
'last':'cur',
'location':'paris'
}
}
for username, use_info in users.items():
print('
username: '+ username)
fullname = use_info['first'] + " " + use_info['last']
location = use_info['location']
print(" Full name: " + fullname.title())
print(" location: " + location.title())
示例结果: