iT邦幫忙

2021 iThome 鐵人賽

DAY 9
0
Modern Web

基礎python,讓你學點東西系列 第 9

[Day9]-字典(dict)

  • 基本字典
  1. 字典的元素是以”key:value”的方式儲存的,可以利用key去找到對應的value,基本格式如下:
    Dict = {key1:value1,key2:value,…….}
    Key通常是使用字串或數字,且每個key都是唯一值,value可以為任何的資料物件
    https://ithelp.ithome.com.tw/upload/images/20210921/20140644UHbcqIQEpm.png
    https://ithelp.ithome.com.tw/upload/images/20210921/201406441tbz8Kc03d.png

  2. 刪除字典
    Pop()方法
    https://ithelp.ithome.com.tw/upload/images/20210921/201406444U90ggQiVs.png
    https://ithelp.ithome.com.tw/upload/images/20210921/20140644Zy4bJ1FAHW.png
    Popitem() 可以隨機刪除
    https://ithelp.ithome.com.tw/upload/images/20210921/20140644117XBtecy0.png
    https://ithelp.ithome.com.tw/upload/images/20210921/20140644YiMg7rzN8i.png

  3. Dict() 有時候會碰到雙值序列,可以是dict()轉換成字典,第一個就是key,第二個就是value
    https://ithelp.ithome.com.tw/upload/images/20210921/20140644zsCus5ihyN.png
    https://ithelp.ithome.com.tw/upload/images/20210921/201406441bW11fTce2.png

  4. key in dict 驗證元素是否存在
    https://ithelp.ithome.com.tw/upload/images/20210921/20140644aqjrjL3esL.png
    https://ithelp.ithome.com.tw/upload/images/20210921/20140644m7Xu5mb3Py.png
    https://ithelp.ithome.com.tw/upload/images/20210921/20140644njDCduJHBp.png
    https://ithelp.ithome.com.tw/upload/images/20210921/20140644SOwcWN3AgN.png


上一篇
[Day8]-元組(tuple)
下一篇
[Day10]-字典2
系列文
基礎python,讓你學點東西30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言