python如何修改字典中的值
小妮浅浅
2021-10-28 10:07:541423浏览 · 0收藏 · 0评论
1,修改字典中的值,可以依次指定字典名称,用方括号括起的键和与键相关的新值。
>>> bullet = {'color': 'green', 'points': '5', 'bullet_x': 25, 'bullet_y': 45} >>> bullet['bullet_x']=10 >>> print(bullet) {'color': 'green', 'points': '5', 'bullet_x': 10, 'bullet_y': 45} >>>
2,对于字典中不再需要的信息,可以用del语句彻底删除相应的键值对。
>>> bullet = {'color': 'green', 'points': '5', 'bullet_x': 25, 'bullet_y': 45} >>> bullet['bullet_x']=10 >>> print(bullet) {'color': 'green', 'points': '5', 'bullet_x': 10, 'bullet_y': 45} >>> del bullet['color'] >>> print(bullet) {'points': '5', 'bullet_x': 10, 'bullet_y': 45} >>>
以上就是python修改字典中值的方法,希望对大家有所帮助。更多Python学习指路:python基础教程
本文教程操作环境:windows7系统,Python 3.9.1,DELL G3电脑。
关注公众号,随时随地在线学习
相关文章