Python-Day2-Dictionary

# info = {
# 'stu1':'Ivy',
# 'stu2':'carol',
# 'stu3':'Green'
# }
# b = {
# 'stu1': 'Will',
# 1:3,
# 2:5
# }
# info.update(b)#合并加更改
# iterms
#print(info.items())
#print(info)
#初始化字典,有keyvalue
c = dict.fromkeys([6,7,8],[1,{"name": "Alex"},444])
c[7][1]['name'] = "Jack chen"
print(c)
# print(info)
# print(info['stu1']) #key value 查询
# print(info.get('stu03'))#查找,没有返回none
# #修改
# info["stu1"] = "高平"
# info["stu4"] = "Fred"
# print(info)
# print('stu04' in info)# info.has_key Python2 only
# #delte
# info.pop("stu1")
# del info['stu4']
# print(info)
#
# #嵌套
# information = {
# "001":{"湖北":[1,2]},
# "002":{"陕西":[1,3]},
# "003":{"河北":[2,3]},
# }
# #修改
#
# information.setdefault("004",{"山西":[1,2]})
# print(information)

info = {
'stu1':'Ivy',
'stu2':'carol',
'stu3':'Green'
}
#循环, 第一个效率高
for i in info:
print(i,info[i])
for k,v in info.items():
print(k,v)

posted @ 2020-05-05 08:12  Carol7258  阅读(146)  评论(0)    收藏  举报