python 字典的使用

增删改查

info={
    'stu001':'wang',
    'stu002':'li',
    'stu003':'du',
    'stu004':'song',
    'stu005':'cun',
    'stu006':'chen'
}
print(info.get("stu007")) #
一般用这种查找方式,没有的时候返回为none
print('stu003' in info) #查找info里面有没有这个学号,没有的话返回为false. 和python2中的 info.has_key("002")相同

info['stu001']="王" #修改里面的内容
info["stu007"]="chi" #如果stu007不存在则自动加载进去
#删除操作
#del info["stu007"]
info.pop("stu007")  #将这个删除
print(info)
info.popitem() #随机删除
print(info)
#print(info['stu003']) #将stu003的人取出来
原文地址:https://www.cnblogs.com/hanhan914-wang/p/7424221.html