30,339
社区成员
发帖
与我相关
我的任务
分享
#将川川改为川川菜鸟
thisdict = {
"name": "川川",
"address": "上海",
"year": 2000
}
thisdict['name'] = '川川菜鸟'
print(thisdict)
#或者我们使用update()方法:
thisdict = {
"name": "川川",
"address": "上海",
"year": 2000
}
thisdict.update({'name': '川川菜鸟'})
print(thisdict)
#比如我要添加一个年龄为20:
thisdict={
'name': '故人',
'adress': '河南',
'year':2002
}
thisdict['age']=20
print(thisdict)
#或者还是使用update:
thisdict={
'name': '故人',
'adress': '河南',
'year':2002
}
thisdict.update({'age':'19岁'})
print(thisdict)
# pop()方法删除具有指定键名的项
#比如我要删除地址项目
thisdict={
'name': '故人',
'adress': '河南',
'year':2002
}
thisdict.pop('adress')
print(thisdict)
#popitem()方法删除最后插入的项目(在3.7之前的版本中,将删除随机项目)
thisdict={
'name': '故人',
'adress': '河南',
'year':2002
}
thisdict.popitem()
print(thisdict)
#del关键字删除与指定键名称的项目:
thisdict={
'name': '故人',
'adress': '河南',
'year':2002
}
del thisdict['name']
print(thisdict)
# del关键字也可以删除字典完全
del thisdict
# clear()方法清空字典
thisdict={
'name': '故人',
'adress': '河南',
'year':2002
}
thisdict.clear()
print(thisdict)