1. 增加:字典[key]=value(不存在的key和value)
info={ 'stu1101':'TengLan', 'stu1102':'LuoZe', 'stu1103':'XiaoZe' } info['stu1104']='crystal' print(info) #输出结果:{'stu1101': 'TengLan', 'stu1102': 'LuoZe', 'stu1103': 'XiaoZe', 'stu1104': 'crystal'}
2. 修改:字典[key]=value(已存在的key,修改对应value)
info={ 'stu1101':'TengLan', 'stu1102':'LuoZe', 'stu1103':'XiaoZe' } info['stu1101']='crystal' print(info) #输出结果:{'stu1101': 'crystal', 'stu1102': 'LuoZe', 'stu1103': 'XiaoZe'}
3. 删除
a. 字典.pop(key)
info={ 'stu1101':'TengLan', 'stu1102':'LuoZe', 'stu1103':'XiaoZe' } info.pop("stu1101") print(info) #{'stu1102': 'LuoZe', 'stu1103': 'XiaoZe'}
b. del 字典[key]
info={ 'stu1101':'TengLan', 'stu1102':'LuoZe', 'stu1103':'XiaoZe' } del info['stu1101'] print(info)
4. 查找
a. in:如果key存在,返回true;反之返回false
info={ 'stu1101':'TengLan', 'stu1102':'LuoZe', 'stu1103':'XiaoZe' } print("stu1101" in info) #输出结果:True
b.get:通过key获取到value,如果不存在返回None
info={ 'stu1101':'TengLan', 'stu1102':'LuoZe', 'stu1103':'XiaoZe' } print(info.get("stu1101")) #输出结果:TengLan
c. 字典[key]:如果key存在,返回value;反之则报错
info={ 'stu1101':'TengLan', 'stu1102':'LuoZe', 'stu1103':'XiaoZe' } print(info["stu1104"]) #输出结果:KeyError: 'stu1104'
5. 多级字典嵌套操作
av_catalog = { "Europe":{ "Germany":["地域广","文化浓郁"], "United Kingdom":["历史古国","高校多"], "Italy":["异域风情","人美"], "Belgium":["时装","文化"] }, "Japan":{ "tokyo":["美食","旅游"] }, "China":{ "Beijing":["国都","历史悠久"] } }
av_catalog["China"]["Beijing"][1]="文明之源"
print(av_catalog) #输出结果:{'Europe': {'Germany': ['地域广', '文化浓郁'], 'United Kingdom': ['历史古国', '高校多'], 'Italy': ['异域风情', '人美'], 'Belgium': ['时装', '文化']}, 'Japan': {'tokyo': ['美食', '旅游']}, 'China': {'Beijing': ['国都', '文明之源']}}
6. setdefault :创建新的key-value
av_catalog = { "Europe":{ "Germany":["地域广","文化浓郁"], "United Kingdom":["历史古国","高校多"], "Italy":["异域风情","人美"], "Belgium":["时装","文化"] }, "Japan":{ "tokyo":["美食","旅游"] }, "China":{ "Beijing":["国都","历史悠久"] } } av_catalog.setdefault("Taiwan",{"travel":[1,2]}) print(av_catalog) #输出结果:{'Europe': {'Germany': ['地域广', '文化浓郁'], 'United Kingdom': ['历史古国', '高校多'], 'Italy': ['异域风情', '人美'], 'Belgium': ['时装', '文化']}, 'Japan': {'tokyo': ['美食', '旅游']}, 'China': {'Beijing': ['国都', '历史悠久']}, 'Taiwan': {'travel': [1, 2]}}
7. update:两个字典合并,有交叉合并,如没有交叉就更新
info={ 'stu1101':'TengLan', 'stu1102':'LuoZe', 'stu1103':'XiaoZe' } b = { 'stu1101':"crystal", 1:3, 2:5 } info.update(b) print(info) #输出结果:{'stu1101': 'crystal', 'stu1102': 'LuoZe', 'stu1103': 'XiaoZe', 1: 3, 2: 5}
8. fromkeys:初始化一个新的字典
info={ 'stu1101':'TengLan', 'stu1102':'LuoZe', 'stu1103':'XiaoZe' } b = { 'stu1101':"crystal", 1:3, 2:5 } c = info.fromkeys([6,7,8],"test") print(c) #输出结果:{6: 'test', 7: 'test', 8: 'test'}
9. items:把字典转换成列表
info={ 'stu1101':'TengLan', 'stu1102':'LuoZe', 'stu1103':'XiaoZe' } print(info.items()) #输出结果:dict_items([('stu1101', 'TengLan'), ('stu1102', 'LuoZe'), ('stu1103', 'XiaoZe')])
10. 字典循环
info={ 'stu1101':'TengLan', 'stu1102':'LuoZe', 'stu1103':'XiaoZe' } for i in info: #此方式循环高效 print(i,info[i]) for k,v in info.items(): #此方式不适合数据量较大的情况 print(k,v) #输出结果: stu1101 TengLan stu1102 LuoZe stu1103 XiaoZe stu1101 TengLan stu1102 LuoZe stu1103 XiaoZe