iT邦幫忙

2021 iThome 鐵人賽

DAY 10
0
Modern Web

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

[Day10]-字典2

  • 遍歷字典
  1. Items() 可以取得key跟value
    https://ithelp.ithome.com.tw/upload/images/20210922/20140644u9Sa7wgtUh.png
    https://ithelp.ithome.com.tw/upload/images/20210922/201406445ElfpAbMaq.png
  2. Key() 只取得key
    https://ithelp.ithome.com.tw/upload/images/20210922/20140644fXB1vsP1Tv.png
    https://ithelp.ithome.com.tw/upload/images/20210922/20140644gNhOraLFKK.png
  3. Values() 只取得value
    https://ithelp.ithome.com.tw/upload/images/20210922/20140644mCuaU0bnHW.png
    https://ithelp.ithome.com.tw/upload/images/20210922/2014064423QNkxMkT9.png
  • 常見的函數與用法
  1. len() 可以取得元素的個數
    https://ithelp.ithome.com.tw/upload/images/20210922/201406445gAlrgvNK4.png
    https://ithelp.ithome.com.tw/upload/images/20210922/20140644Uo1f1BRxnk.png
  2. fromkeys() 建立字典,如果沒有設定value則會使用none當作value
    https://ithelp.ithome.com.tw/upload/images/20210922/20140644AOiX68eSwK.png
    https://ithelp.ithome.com.tw/upload/images/20210922/201406446xCaj6M4Of.png
  3. get() 搜尋字典的key,如果沒有找到的話就回傳預設值
    https://ithelp.ithome.com.tw/upload/images/20210922/20140644d4oFQ5Y52X.png
    https://ithelp.ithome.com.tw/upload/images/20210922/20140644b0PYIBAyoi.png
  4. setdefault() 搜尋字典的key但跟get()不同的是,如果key不在,他會新增進去字典
    https://ithelp.ithome.com.tw/upload/images/20210922/20140644HKDRPNjjfD.png
    https://ithelp.ithome.com.tw/upload/images/20210922/20140644t7XBW5BDSq.png

上一篇
[Day9]-字典(dict)
下一篇
[Day11]-集合(set)
系列文
基礎python,讓你學點東西30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言