dict及增删查改 02
改
dic1 = {'age':16,'name':'套你大象','sex':',male','weight':'67'} dic2 = {'age':45,'sex':',male'} dic2.update(dic1)#将dict1中的数据更新到dict2中,有更改,没有添加 print(dic1) print(dic2)
dic1 = {'age':16,'name':'套你大象','sex':',male','weight':'67'} dic1['age'] = '19' print(dic1)
查
dic1 = {'age':16,'name':'套你大象','sex':',male','weight':'67'} print(dic1.keys())#键 print(dic1.items())#值 print(dic1.values())#元组,元组中含有键值
dic1 = {'age':16,'name':'套你大象','sex':',male','weight':'67'} # print(dic1.keys())#键 # print(dic1.items())#值 # print(dic1.values())#元组,元组中含有键值 for i in dic1.values(): print(i)
也可以使用for循环来查询,其他两个类似。
dic1 = {'age':16,'name':'套你大象','sex':',male','weight':'67'} l = dic1['age']#直接返回值,但是若字典中没有这个键,则会报错 print(l)