python基础:字典操作
key-value
'''
info = {
'001':"陈翔",
'002':"妹大爷",
'003':"茅台",
'004':"瑞土",
'005':"蘑菇头",
'006':"腿腿",
}#创建
print(info)
print(info['001'])
info["001"] ="吴妈"#修改
info["007"] ="球球"#添加
del
del info["001"]
info.pop("001")
info.popitem()#随机删除
print(info)
'''
#查找
#info['001']#除非确定里面有什么键用
print(info.get('008'))#不确定字典里面的内容
print('012' in info)# =info.has_key("1103")in py2.x去判断字典里有没有这个数据,如果有就取出,没有就创建一个
多级字典嵌套及操作
'''
av_catalog = {
"欧美":{
"www.youporn.com": ["很多免费的,世界最大的","质量一般"],
"www.pornhub.com": ["很多免费的,也很大","质量比youporn高点"],
"letmedothistoyou.com": ["多是自拍,高质量图片很多","资源不多,更新慢"],
"x-art.com": ["质量很高,真的很高","全部收费,屌比请绕过"]
},
"日韩": {
"tokyo-hot": ["质量怎样不清楚,个人已经不喜欢日韩范了","听说是收费的"]
},
"大陆": {
"1024": ["全部免费,真好,好人一生平安","服务器在国外,慢"]
}
}
av_catalog["大陆"]["1024"][1] = "可以在国外做镜像"
av_catalog.setdefault("大陆",{"www.baidu.com":[1]})#当字典里没有这个键,就会创建新增这个键值队,当字典里有这个键,那么原来的还是不变
print(av_catalog)
'''
'''
updata用处
info = {
'001':"陈翔",
'002':"妹大爷",
'003':"茅台",
'004':"瑞土",
'005':"蘑菇头",
'006':"腿腿",
}
b ={
'001':"shanshan",
1:3,
2:5
}
info.update(b)
print(info)
print(info.items())
c=dict.fromkeys([6,7,8],[1,{"name":"chen xiang"},444])
print(c)
c[7][1]['name'] ="陈翔"
print(c)
循环字典
for i in info:#效率高点
print(i,info[i])
for k,v in info.items():
print(k,v)
'''
老师讲的三级菜单
data = {
'北京':{
"昌平":{
"沙河":["oldboy","test"],
"天通苑":["链家地产","我爱我家"]
},
"朝阳":{
"望京":["奔驰","陌陌"],
"国贸":{"CICC","HP"},
"东直门":{"Advent","飞信"},
},
"海淀":{},
},
'山东':{
"德州":{},
"青岛":{},
"济南":{}
},
'广东':{
"东莞":{},
"常熟":{},
"佛山":{},
},
}
exit_flag = False
while not exit_flag:
for i in data:
print(i)
choice = input("选择进入>>:")
if choice in data:
while not exit_flag:
for i2 in data[choice]:
print("\t",i2)
choice2 = input("选择进入2>>:")
if choice2 in data[choice]:
while not exit_flag:
for i3 in data[choice][choice2]:
print("\t",i3)
choice3 = input("选择进入3>>:")
if choice3 in data[choice][choice2]:
for i4 in data[choice][choice2][choice3]:
print(i4)
choice4 = input("最后一层,按b返回>>:")
if choice4 == "b":
pass
elif choice4 =="q":
exit_flag= True
if choice3 =="b":
break
elif choice3 =="q":
exit_flag= True
if choice2 =="b":
break
elif choice2 =="q":
exit_flag= True