【Python基礎語法04】數據結構之字典

前言

上一講分享了數據結構中的元組 tuple,本講將走進字典 dict。採用 Q & A 的形式講解幾點思考並採用腦圖的形式進行總結。

Q1:什麼是 dict

dict 是存儲數據的容器之一,是無序的可變映射容器 map;由若干組鍵值對 {key : value} 構成,用花括號標識,每對鍵值對之間用逗號隔開,key 與 value 之間用冒號隔開。key 是唯一的,不可變;value 是可變的。

>>> dic = {'name':'chen','score':100,'age':18}                                                                          
>>> print(dic,type(dic))                                                                                                
{'name': 'chen', 'score': 100, 'age': 18} <class 'dict'> 

Q2:爲什麼設置 dict

list 與 tuple 都是序列 seq,這裏的有序是指先後有順序,即索引 index;seq 缺少一對一的關聯屬性,而 map 恰恰彌補了這一點。

Q3:如何訪問 dict 中的元素

seq 中藉助 index, slice, stride 實現,map 中藉助 key 即可。此外,dict 還有一些內置方法可以快捷的訪問 dict 的 key, value, (key, value)。注意嵌套 dict 的重複指向 key, 這與後期 Numpy 中的多維數組訪問類似。

>>> dic = {'name':'chen','score':100,'age':18,'other':{'height':188}}                                                   
>>> print(dic['name'])                                                                                                  
chen                                                                                                                    
>>> print(dic['other']['height'])                                                                                       
188 

下表展示了訪問 dict 的常用內置方法。

方法 描述 實例
dic.get(key) 返回 key 的值 >>> dic = {‘name’:‘chen’,‘score’:100,‘age’:18,‘other’:{‘height’:188}}
>>> print(dic.get(‘name’))
chen
dic.keys() 返回 dic 的所有 key >>> dic = {‘name’:‘chen’,‘score’:100,‘age’:18,‘other’:{‘height’:188}}
>>> print(dic.keys())
dict_keys([‘name’, ‘score’, ‘age’, ‘other’])
dic.values() 返回 dic 的所有 value >>> dic = {‘name’:‘chen’,‘score’:100,‘age’:18,‘other’:{‘height’:188}}
>>> print(dic.values())
dict_values([‘chen’, 100, 18, {‘height’: 188}])
dic.items() 返回 dic 的所有元素 >>> dic = {‘name’:‘chen’,‘score’:100,‘age’:18,‘other’:{‘height’:188}}
>>> print(dic.items())
dict_items([(‘name’, ‘chen’), (‘score’, 100), (‘age’, 18), (‘other’, {‘height’: 188})])

Q4:如何修改 dict

與 seq 類似,修改 list 也涉及到一些常用的內置函數與方法。內置函數與 seq 類似,即 del, len(dict), in等。
下表列舉了常用的內置方法。

方法 描述 實例
dic.clear() 清空 dic >>> dic = {‘name’:‘chen’,‘score’:100,‘age’:18,‘other’:{‘height’:188}}
>>> print(dic.clear())
None
dic.pop(key) 移除 dic 中 key 對應的值 >>> dic = {‘name’:‘chen’,‘score’:100,‘age’:18,‘other’:{‘height’:188}}
>>> print(dic.pop(‘age’))
18
>>> print(dic)
{‘name’: ‘chen’, ‘score’: 100, ‘other’: {‘height’: 188}}
dic.copy() 複製 dic >>> dic = {‘name’:‘chen’,‘score’:100,‘age’:18,‘other’:{‘height’:188}}
>>> dic2 = dic.copy()
>>> print(dic2)
{‘name’: ‘chen’, ‘score’: 100, ‘age’: 18, ‘other’: {‘height’: 188}}
dict.fromkeys(seq) 將 seq 元素當作 key 創建 dict >>> seq = (‘name’,‘score’,‘age’)
>>> dic = dict.fromkeys(seq)
>>> print(dic)
{‘name’: None, ‘score’: None, ‘age’: None}
>>> dic = dict.fromkeys(seq,10)
>>> print(dic)
{‘name’: 10, ‘score’: 10, ‘age’: 10}
dic1.update(dic2) 將 dic2 更新到 dic1 中 >>> dic1 = {“a”:1, “b”:2}
>>> dic2 = {“c”:3, “d”:4}
>>> dic1.update(dic2)
>>> print(dic1,dic2)
{‘a’: 1, ‘b’: 2, ‘c’: 3, ‘d’: 4} {‘c’: 3, ‘d’: 4}

腦圖總結

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章