PHP前端开发

python中怎么向字典添加元素

百变鹏仔 1个月前 (01-22) #Python
文章标签 字典
python中向字典添加元素的方法:可以通过给定键值对直接向字典中添加元素,如【aa['价格'] = 100 aa['价格'] = 100】。

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

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

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

方法二:使用update方法

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