编程语言
首页 > 编程语言> > python字典添加元素和删除元素

python字典添加元素和删除元素

作者:互联网

1. 添加字典元素

方法一:直接添加,给定键值对

#pycharm
aa = {'人才':60,'英语':'english','adress':'here'}
print(aa) # {'人才': 60, '英语': 'english', 'adress': 'here'}
#添加方法一:根据键值对添加
aa['价格'] = 100
print(aa) # {'人才': 60, '英语': 'english', 'adress': 'here', '价格': 100}

方法二:使用update方法

#添加方法二:使用update方法添加
xx = {'hhh':'gogogo'}
aa.update(xx)
print(aa) # {'人才': 60, '英语': 'english', 'adress': 'here', '价格': 100, 'hhh': 'gogogo'}

2. 删除字典元素

方法一:del函数

# 删除方法一:使用del函数
del[aa['adress']]
print(aa) # {'人才': 60, '英语': 'english', '价格': 100, 'hhh': 'gogogo'}

方法二:pop函数

#删除方法二:使用pop函数,并返回值
vv = aa.pop('人才')
print(vv) # 60
print(aa) # {'英语': 'english', '价格': 100, 'hhh': 'gogogo'}

方法三:clear函数

# clear方法,删除所有
aa.clear()
print(aa) # {},为空

 

标签:aa,python,元素,60,english,print,100,adress,字典
来源: https://www.cnblogs.com/qi-yuan-008/p/11862815.html