本文摘自php中文网,作者V,侵删。
python中向字典添加元素的方法:可以通过给定键值对直接向字典中添加元素,如【aa['价格'] = 100 aa['价格'] = 100】。

方法一:直接添加,给定键值对
(推荐教程:python视频教程)
1 2 3 4 5 6 | #pycharm
aa = { '人才' :60, '英语' : 'english' , 'adress' : 'here' }
print (aa) # { '人才' : 60, '英语' : 'english' , 'adress' : 'here' }
#添加方法一:根据键值对添加
aa[ '价格' ] = 100
aa[ '价格' ] = 100 # { '人才' : 60, '英语' : 'english' , 'adress' : 'here' , '价格' : 100}
|
方法二:使用update方法
1 2 3 4 | #添加方法二:使用update方法添加
xx = { 'hhh' : 'gogogo' }
aa.update(xx)
print (aa) # { '人才' : 60, '英语' : 'english' , 'adress' : 'here' , '价格' : 100, 'hhh' : 'gogogo' }
|
相关推荐:python教程
以上就是python中怎么向字典添加元素的详细内容,更多文章请关注木庄网络博客!!
相关阅读 >>
Python list是否包含另一个list所有元素
怎么在cmd运行Python
Python如何发送邮件
Python如何另起一行
Python如何对比图像的区别
对Python中yield和generators的深度解析
Python数据分析师需要学什么
使用xcode怎么编写并运行Python?
Python序列之列表
Python怎么运行
更多相关阅读请进入《Python》频道 >>
人民邮电出版社
python入门书籍,非常畅销,超高好评,python官方公认好书。
转载请注明出处:木庄网络博客 » python中怎么向字典添加元素