方法 | 描述 |
---|---|
dict.get(key[, default]) | 從 dict 中取得 key 的 value ,若無此 key 則回傳 default , default 預設為 None |
舉例示範如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | d1 = { 1 : "a" , 2 : "b" , 3 : "c" } print (d1) t1 = d1.get( 2 ) print (t1) print () d2 = { 4 : "d" , 5 : "e" , 6 : "f" } print (d2) t2 = d2.get( 2 ) print (t2) print () d3 = { 7 : "g" , 8 : "h" , 9 : "i" } print (d3) t3 = d3.get( 2 , "not found" ) print (t3) print () # 《程式語言教學誌》的範例程式 # 檔名:get.py # 功能:示範 Python 程式 # 作者:張凱慶 # 時間:西元 2010 年 12 月 |
執行結果如下

中英文術語對照 | |
---|---|
字典 | dictionary |
型態 | type |
方法 | method |
沒有留言:
張貼留言