1. 程式人生 > >Python學習之路-字典dict常用方法

Python學習之路-字典dict常用方法

round 存在 set get 輸出 查詢 val 沒有 span

字典特性:

  • dict無序
  • key唯一,天生去重

創建字典:

way1:小心列表坑

 1 __author__ = "KuanKuan"
 2 d = dict.fromkeys([1, 2, 3], ["name", "age"])
 3 print("字典:", d)
 4 
 5 d[1][0] = "mmph好坑"
 6 print("修改後:",d)
 7 """
 8 輸出:
 9 字典: {1: [‘name‘, ‘age‘], 2: [‘name‘, ‘age‘], 3: [‘name‘, ‘age‘]}
10 修改後: {1: [‘mmph好坑‘, ‘age‘], 2: [‘mmph好坑‘, ‘age‘], 3: [‘mmph好坑‘, ‘age‘]}
11 12 """

way2:

 1 info = {"teacher1":"蒼井空",
 2         "teacher2":"京香",
 3         "teacher3":"波多野結衣",
 4         "teacher4":"小澤瑪利亞"
 5         }
 6 print("你懂得:",info)
 7 """
 8 輸出:
 9 你懂得: {‘teacher1‘: ‘蒼井空‘, ‘teacher2‘: ‘京香‘, ‘teacher3‘: ‘波多野結衣‘, ‘teacher4‘: ‘小澤瑪利亞‘}
10 """

字典無序輸出

查詢

print
(info["teacher1"])#如果不存在會報錯 print(info.get("teacher5"))#推薦用這種方式如果key不存在則返回None print("teacher1" in info)#如果存在返回True print("查詢字典的keys:",info.keys())#查詢字典的keys print("查詢字典的values:",info.values())#查詢字典的values print(info.items()) """ 蒼井空 None True 查詢字典的keys: dict_keys([‘teacher1‘, ‘teacher2‘, ‘teacher3‘, ‘teacher4‘]) 查詢字典的values: dict_values([‘蒼井空‘, ‘京香‘, ‘波多野結衣‘, ‘小澤瑪利亞‘]) dict_items([(‘teacher1‘, ‘蒼井空‘), (‘teacher2‘, ‘京香‘), (‘teacher3‘, ‘波多野結衣‘), (‘teacher4‘, ‘小澤瑪利亞‘)])
"""

修改

1 info["teacher1"] = "天海翼"
2 print("修改後:",info)
3 #修改後: {‘teacher1‘: ‘天海翼‘, ‘teacher2‘: ‘京香‘, ‘teacher3‘: ‘波多野結衣‘, ‘teacher4‘: ‘小澤瑪利亞‘}

增加

 1 info["teacher1"] = "天海翼"
 2 print("修改後:",info)
 3 #修改後: {‘teacher1‘: ‘天海翼‘, ‘teacher2‘: ‘京香‘, ‘teacher3‘: ‘波多野結衣‘, ‘teacher4‘: ‘小澤瑪利亞‘}
 4 info["teacher5"] = "上原瑞惠"#沒有則自動創立
 5 print("增加後的字典:",info)
 6 info.setdefault("teacher1","櫻井莉亞")#存在則不會被修改
 7 info.setdefault("teacher6","櫻井莉亞")
 8 print(info)
 9 b = {"teacher1":"蒼井空",
10      "teacher7":"桃谷繪裏香"
11      }
12 info.update(b)
13 print("修改後的:",info)
14 """
15 修改後: {‘teacher1‘: ‘天海翼‘, ‘teacher2‘: ‘京香‘, ‘teacher3‘: ‘波多野結衣‘, ‘teacher4‘: ‘小澤瑪利亞‘}
16 增加後的字典: {‘teacher1‘: ‘天海翼‘, ‘teacher2‘: ‘京香‘, ‘teacher3‘: ‘波多野結衣‘, ‘teacher4‘: ‘小澤瑪利亞‘, ‘teacher5‘: ‘上原瑞惠‘}
17 {‘teacher1‘: ‘天海翼‘, ‘teacher2‘: ‘京香‘, ‘teacher3‘: ‘波多野結衣‘, ‘teacher4‘: ‘小澤瑪利亞‘, ‘teacher5‘: ‘上原瑞惠‘, ‘teacher6‘: ‘櫻井莉亞‘}
18 修改後的: {‘teacher1‘: ‘蒼井空‘, ‘teacher2‘: ‘京香‘, ‘teacher3‘: ‘波多野結衣‘, ‘teacher4‘: ‘小澤瑪利亞‘, ‘teacher5‘: ‘上原瑞惠‘, ‘teacher6‘: ‘櫻井莉亞‘, ‘teacher7‘: ‘桃谷繪裏香‘}
19 """

刪除

 1 del info["teacher1"]
 2 print("刪除後的:",info)
 3 info.pop("teacher2")
 4 print("刪除後的2:",info)
 5 info.popitem()
 6 print("隨機刪除後的:",info)
 7 """
 8 刪除後的: {‘teacher2‘: ‘京香‘, ‘teacher3‘: ‘波多野結衣‘, ‘teacher4‘: ‘小澤瑪利亞‘, ‘teacher5‘: ‘上原瑞惠‘, ‘teacher6‘: ‘櫻井莉亞‘, ‘teacher7‘: ‘桃谷繪裏香‘}
 9 刪除後的2: {‘teacher3‘: ‘波多野結衣‘, ‘teacher4‘: ‘小澤瑪利亞‘, ‘teacher5‘: ‘上原瑞惠‘, ‘teacher6‘: ‘櫻井莉亞‘, ‘teacher7‘: ‘桃谷繪裏香‘}
10 隨機刪除後的: {‘teacher3‘: ‘波多野結衣‘, ‘teacher4‘: ‘小澤瑪利亞‘, ‘teacher5‘: ‘上原瑞惠‘, ‘teacher6‘: ‘櫻井莉亞‘}
11 
12 """

遍歷

for i in info:
    print(i,info[i])
print("*"*50)
for key,value in info.items():#先轉列表,在遍歷,如果字典過大,會變慢
    print(key,value)
"""
teacher3 波多野結衣
teacher4 小澤瑪利亞
teacher5 上原瑞惠
teacher6 櫻井莉亞
**************************************************
teacher3 波多野結衣
teacher4 小澤瑪利亞
teacher5 上原瑞惠
teacher6 櫻井莉亞
"""

清空

1 info.clear()
2 print("清空後的:",info)
3 #清空後的: {}

Python學習之路-字典dict常用方法