dict及增删查改 02

dic1 = {'age':16,'name':'套你大象','sex':',male','weight':'67'}
dic2 = {'age':45,'sex':',male'}
dic2.update(dic1)#将dict1中的数据更新到dict2中,有更改,没有添加
print(dic1)
print(dic2)
View Code
dic1 = {'age':16,'name':'套你大象','sex':',male','weight':'67'}
dic1['age'] = '19'
print(dic1)
View Code

 查

dic1 = {'age':16,'name':'套你大象','sex':',male','weight':'67'}
print(dic1.keys())#
print(dic1.items())#
print(dic1.values())#元组,元组中含有键值
View Code
dic1 = {'age':16,'name':'套你大象','sex':',male','weight':'67'}
# print(dic1.keys())#键
# print(dic1.items())#值
# print(dic1.values())#元组,元组中含有键值

for i in dic1.values():
    print(i)
View Code

也可以使用for循环来查询,其他两个类似。

dic1 = {'age':16,'name':'套你大象','sex':',male','weight':'67'}
l = dic1['age']#直接返回值,但是若字典中没有这个键,则会报错
print(l)
View Code

 

posted @ 2019-07-21 22:28  套你大象  阅读(130)  评论(0编辑  收藏  举报