2022年 11月 4日

python系列之:字典update

python系列之:字典update

  • 一、字典不存在键值update
  • 二、字典已经存在键值update

一、字典不存在键值update

字典不存在键值,使用update会添加键值对到字典中

account = "optics"
decrypt_passwd = "12345abc"

accountDict = {}
accountDict.update({account:decrypt_passwd})
print(accountDict)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

输出如下所示:
{‘optics’: ‘12345abc’}

二、字典已经存在键值update

字典已经存在键值update会更新键的值

account = "optics"
decrypt_passwd = "abcdef"

accountDict = {'optics': '12345abc'}
accountDict.update({account:decrypt_passwd})
print(accountDict)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

输出如下所示:
{‘optics’: ‘abcdef’}