python基础入门---字典操作

#key-value
info = {
    ‘stu1101‘: "luyi",
    ‘stu1102‘: "yuanjinxia",
    ‘stu1103‘: "situomo",
}
for i in info:
    print(i,info[i])

for k,v in info.items():#先转成列表再打印
    print(k,v)


‘‘‘
b = {
    ‘stu1101‘:"qjh",
    1:3,
    2:5
}
info.update(b)#更新操作
#print(info)  #字典打印出来是无序的,key必须是唯一的,天生去重
#print(info["stu1101"])
#info["stu1101"] = "lichanggeng"
#info["stu1104"] = "jinyizhixia"
print(info)
c = dict.fromkeys([6,7,8],[1,{"name":"qjj"},444])
print(c)
c[7][1][‘name‘]="jack"
print(c)
#print(info.items())#把字典转成列表

#del #删除操作
#del info["stu1101"]
#info.pop("stu1101")
#info.popitem()#随便删除
#print(info)

# info [‘stu1103‘]
# print(info.get(‘stu1103‘))
# print(‘stu1108‘in info)
‘‘‘

相关推荐