info={'xihuan01':'张艺兴','xihuan02':'李易峰','xihuan03':'吴亦凡','xihuan04':'黄景瑜','xihuan05':'朴灿烈','xihuan06': '张云龙' } print(info) print(info['xihuan03']) info['xihuan03']='like吴亦凡' print(info) print(info.get('xihuan05'))#提取元素 print(info.get('xihuan07'))#若元素不存在,返回false print('xihuan04' in info)#判断元素是否存在 运行结果 {'xihuan01': '张艺兴', 'xihuan02': '李易峰', 'xihuan03': '吴亦凡', 'xihuan04': '黄景瑜', 'xihuan05': '朴灿烈', 'xihuan06': '张云龙'} 吴亦凡 {'xihuan01': '张艺兴', 'xihuan02': '李易峰', 'xihuan03': 'like吴亦凡', 'xihuan04': '黄景瑜', 'xihuan05': '朴灿烈', 'xihuan06': '张云龙'} 朴灿烈 None True
del info['xihuan02'] print(info) info.pop('xihuan06') print(info) info.popitem() #随意删除 print(info)运行结果 {'xihuan01': '张艺兴', 'xihuan03': 'like吴亦凡', 'xihuan04': '黄景瑜', 'xihuan05': '朴灿烈', 'xihuan06': '张云龙'} {'xihuan01': '张艺兴', 'xihuan03': 'like吴亦凡', 'xihuan04': '黄景瑜', 'xihuan05': '朴灿烈'} {'xihuan01': '张艺兴', 'xihuan03': 'like吴亦凡', 'xihuan04': '黄景瑜'} In [ ]:
actor={'中国':{'何以笙箫默':'钟汉良','微微一笑很倾城':'杨洋','杉杉来了':'张翰'},'韩国':{'太阳的后裔':['宋钟基','宋慧乔'],'W两个世界':['李钟硕','朴信惠'],'坏家伙们':'朴海镇'} } print(actor) actor['韩国']['太阳的后裔'][1]='乔妹' print(actor)输出 {'中国': {'何以笙箫默': '钟汉良', '微微一笑很倾城': '杨洋', '杉杉来了': '张翰'}, '韩国': {'太阳的后裔': ['宋钟基', '宋慧乔'], 'W两个世界': ['李钟硕', '朴信惠'], '坏家伙们': '朴海镇'}} {'中国': {'何以笙箫默': '钟汉良', '微微一笑很倾城': '杨洋', '杉杉来了': '张翰'}, '韩国': {'太阳的后裔': ['宋钟基', '乔妹'], 'W两个世界': ['李钟硕', '朴信惠'], '坏家伙们': '朴海镇'}}
info={'xihuan01':'张艺兴','xihuan02':'李易峰','xihuan03':'吴亦凡','xihuan04':'黄景瑜','xihuan05':'朴灿烈','xihuan06': '张云龙' } a=info.values()#打印所有的值 print(a) b=info.keys()#打印所有的key print(b) info.setdefault('xihuan06','陈立农')#查找key(xihuan07)如果key存在,返回原来的值,如果不存在,创建新的key-value print(info) info.setdefault('xihuan07','陈立农')#查找key(xihuan07)如果key存在,返回原来的值,如果不存在,创建新的key-value print(info)执行结果 dict_values(['张艺兴', '李易峰', '吴亦凡', '黄景瑜', '朴灿烈', '张云龙']) dict_keys(['xihuan01', 'xihuan02', 'xihuan03', 'xihuan04', 'xihuan05', 'xihuan06']) {'xihuan01': '张艺兴', 'xihuan02': '李易峰', 'xihuan03': '吴亦凡', 'xihuan04': '黄景瑜', 'xihuan05': '朴灿烈', 'xihuan06': '张云龙'} {'xihuan01': '张艺兴', 'xihuan02': '李易峰', 'xihuan03': '吴亦凡', 'xihuan04': '黄景瑜', 'xihuan05': '朴灿烈', 'xihuan06': '张云龙', 'xihuan07': '陈立农'}
info={'xihuan01':'张艺兴','xihuan02':'李易峰', } yyy={'xihuan01':'陈立农','xihuan03':'吴亦凡','xihuan04':'黄景瑜',} info.update(yyy)#将yyy添加到info,如果存在相同的key,则覆盖,否则添加 print(info)执行结果 {'xihuan01': '陈立农', 'xihuan02': '李易峰', 'xihuan03': '吴亦凡', 'xihuan04': '黄景瑜'}
info={'xihuan01':'张艺兴','xihuan02':'李易峰', } print(info.items())#将字典转化为元祖 执行结果>>> dict_items([('xihuan01', '张艺兴'), ('xihuan02', '李易峰')])
初始化一个字典 c=dict.fromkeys([1,2,3],'test1') print(c) 执行结果 {1: 'test', 2: 'test', 3: 'test'}c=dict.fromkeys([1,2,3],['test1','test2','test3']) print(c)执行结果 {1: ['test1', 'test2', 'test3'], 2: ['test1', 'test2', 'test3'], 3: ['test1', 'test2', 'test3']}c[3][1]='ok'#改一个全改了 print(c) 执行结果 {1: ['test1', 'ok', 'test3'], 2: ['test1', 'ok', 'test3'], 3: ['test1', 'ok', 'test3']}
方法一 info={'xihuan01':'张艺兴','xihuan02':'李易峰','xihuan03':'吴亦凡','xihuan04':'黄景瑜','xihuan05':'朴灿烈' } for i in info:print(i,info[i]) 执行结果 xihuan01 张艺兴 xihuan02 李易峰 xihuan03 吴亦凡 xihuan04 黄景瑜 xihuan05 朴灿烈方法二 for k,v in info.items():print(k,v) 执行结果 xihuan01 张艺兴 xihuan02 李易峰 xihuan03 吴亦凡 xihuan04 黄景瑜 xihuan05 朴灿烈