python中怎么向字典添加元素

python中向字典添加元素的方法:可以通过给定键值对直接向字典中添加元素,如【aa['价格'] = 100 aa['价格'] = 100】。

python中怎么向字典添加元素

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

(推荐教程:python视频教程)

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

方法二:使用update方法

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

相关推荐:python教程

以上就是python中怎么向字典添加元素的详细内容,更多请关注https://www.sxiaw.com/其它相关文章!