在 Python 中更新字典


您可以透過下面簡單示例中所示的方式新增新條目或鍵值對、修改現有條目或刪除現有條目來更新字典 −

示例

 即時演示

#!/usr/bin/python
dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}
dict['Age'] = 8; # update existing entry
dict['School'] = "DPS School"; # Add new entry
print "dict['Age']: ", dict['Age']
print "dict['School']: ", dict['School']

輸出

執行上述程式碼時,會產生以下結果 −

dict['Age']: 8
dict['School']: DPS School

更新於:2020-01-28

578 次瀏覽

開啟您的 職業生涯

完成課程獲得認證

開始
廣告
© . All rights reserved.