【Python】Python - 修改Dictionary items。Python Change Dictionary Items.

 【Python】Python - 修改Dictionary items。Python Change Dictionary Items.


修改items 的value

你可以使用key值取出item,然後修改他的值:

範例

將 "year" 改為 2018:

thisdict = {
  "brand""Ford",
  "model""Mustang",
  "year"1964
}
thisdict["year"] = 2018
上面輸出: {'brand': 'Ford', 'model': 'Mustang', 'year': 2018}

更新 Dictionary

使用 update() 方法可以去更新dictionary的items。update()方法為dictionary內建的方法。

範例

使用update() 方法去將"year"更新:

thisdict = {
  "brand""Ford",
  "model""Mustang",
  "year"1964
}
thisdict.update({"year"2020})
上面輸出: {'brand': 'Ford', 'model': 'Mustang', 'year': 2020}





留言

這個網誌中的熱門文章

【多益】現點現做的英文怎麼說呢?

《Microsoft Word 應用》:圖片被文字蓋住解決方法,不可設定為固定行高

UBUNTU中轉換磁碟partition 從 MBR轉為GPT,不損失資料