字典处理练习

#!/usr/bin/env python
info = {
'stu1101': "TengLan Wu",
'stu1102': "LongZe Luola",
'stu1103': "XiaoZe Maliya",
}
print(info)
print(info['stu1101'])
info['stu1101']='武藤兰'
info['stu1104']='Mr.Lian'
print(info)
#del info['stu1101']
#info.pop('stu1102')
#info.popitem()#随机删除
print(info.get('stu1105'))#无输出None,有输出值
print('stu1104' in info)#判断有无


av_catalog = {
"欧美":{
"www.youporn.com": ["很多免费的,世界最大的","质量一般"],
"www.pornhub.com": ["很多免费的,也很大","质量比yourporn高点"],
"letmedothistoyou.com": ["多是自拍,高质量图片很多","资源不多,更新慢"],
"x-art.com":["质量很高,真的很高","全部收费,屌比请绕过"]
},
"日韩":{
"tokyo-hot":["质量怎样不清楚,个人已经不喜欢日韩范了","听说是收费的"]
},
"大陆":{
"1024":["全部免费,真好,好人一生平安","服务器在国外,慢"]
}
}
info.values()
info.keys()
av_catalog.setdefault('大陆',{'www.baidu.com':[1,2]})#如果字典里有,则取字典里值,如果没有则赋值
print(av_catalog)

b ={
'stu1101':'Mr.Lian',
1:2,
3:5
}
info.update(b)#字典有则更新,无则添加
print(info)
print(info.items())#把字典转成列表
c =dict.fromkeys([6,7,8],[1,{'name':'lian'},444])#初始化一个新的字典,第二层共用地址
c[7][1]['name']='rongye'
print(c)

for i in info:#速度比下面快
print(i,info[i])
for k,v in info.items() :
print(k,v)
posted @ 2018-11-04 19:27  rongye  阅读(186)  评论(0编辑  收藏  举报