#创建字典
alien_0={'color':'green','points':5}
#添加键值对
alien_0['x_position']=0
alien_0['y_position']=25
print(alien_0)
#创建空字典
alien_0={}
#删除键值对
del alien_0['points']
#遍历字典
user_0={
'username':'efermi',
'first':'enrico',
'last':'fermi',
}
print(user_0)
for key,value in user_0.items():
print("\nKey:"+key)
print("Value:"+value)
#按顺序遍历字典中所有键
favorite_languages = {
'jen':'python' ,
'sarah':'c',
'edward':'ruby',
'phil':'python',
}
for name in sorted(favorite_languages):
print(name.title()+",thank you for taking the poll")
#遍历字典中所有值
for value in favorite_languages.values():
print(value)
#!!剔除字典值中重复项
for value in set(favorite_languages.values()):
print (value.title())
#嵌套:
##字典列表
alien_0={'color':'green','points':5}
alien_1={'color':'yellow','points':10}
alien_2={'color':'red','points':15}
aliens=[alien_0,alien_1,alien_2]
for alien in aliens:
print(alien)
##在字典中存储列表
pizza = {
'crust':'thick',
'toppings':['mushrooms','extra cheese'],
}
##在字典中存储字典
users = {
'aeinstein':{
'first':'albert',
'last':'einstein',
'location':'princeton',
},
'mcurie':{
'first':'marie',
'last':'curie',
'location':'paris',
},
}