python_字典的使用
'''
info = {
"stu1":"lie","stu2":"weite","stu3":"potty"
}
info["stu1"]= "lee" #修改字典中键-值对
info["stu4"]="monica" #增加字典
# del info["stu1"] #删除字典,换个方式删除
# info.pop("stu2") #删除,标准删除
# info.popitem() #随机删除
#查找
#info['stu1']
print(info.get('stu11')) #查找,有就返回值,没有就返回none
print("stu5" in info) #info.has_key("stu5") in py2.x 查找
'''
#字典嵌套 键最好不用中文
china = {
"江苏省":{
"南京市":{"江宁":"a","栖霞":"b","奥体":"c"},
"苏州市":{"吴中":"d","虎丘":"e","阳澄湖":"f"},
"扬州市":{"瘦西湖","东关街","八宝亭"}
},
"浙江省":{
"杭州市":{"西湖":"g","宋城":"h","千岛湖":"i"},
"湖州市":{"莫干山":"j","绍兴":"k"}
}
}
china["浙江省"]["湖州市"]["莫干山"] = '嵌套'
china.setdefault("*",{"台北":["sq","re"]}) #首先找“*”这个值,找不到就新建
#update将info更新,重复的值覆盖,没有的键将新增
'''
info = {
"stu1":"lie","stu2":"weite","stu3":"potty"
}
info2 = {2:3,4:5,"stu1":"alex"}
info.update(info2)
print(info)
c = info.fromkeys([6,7,8],[1,"test",{"name":"jack"}]) #初始化新的字典,键为6,7,8,如果没有赋值,值为none
print(c)
c [7][2]["name"] = "luna" #相当于copy,修改一个值会将字典中所以的值都修改了。三个键共享一个内存地址
print(c)
print(info.items()) #将字典转换成了列表
#print(china)
'''
#字典的循环
info = {
"stu1":"lie","stu2":"weite","stu3":"potty"
}
for i in info: #效率优于下面的方法
print(i,info[i])
for x,y in info.items():
print(x,y)