在介紹完元組和列表之後,我們接著介紹其他的數據結構。
4.1 字典元組和列表有一個共同點:都是利用數字編號來訪問裡面的值。但如果我們想用其他的元素(比如字符串)來當作索引呢?這種情形也是很常見的,比如我們利用Python儲存電話號碼,我們肯定很想用名字來進行索引,訪問取值(電話號碼)。而字典可以解決這個問題。
字典(dictionary)是Python中唯一的內置映射類型。映射是指的可以通過名稱來訪問各個值的數據結構。字典可能是Python裡面最為重要的數據結構。下面我們來介紹關於字典的一些操作。
可以使用大括號來創建字典,用冒號分隔索引和值,用逗號分隔不同的索引和值:
In [1]: dict1 = {'a' : 'stat', 5 : [1,2,3,4]}
In [2]: dict1Out[2]: {'a': 'stat', 5: [1, 2, 3, 4]}
In [3]: type(dict1)Out[3]: dict其中type函數用來表示數據的類型。
同時也可以使用dict函數創建字典:
In [4]: tuple1 = ('yang','li','wang')
In [5]: tuple2 = (100,99,98)
In [6]: dict2 = dict(zip(tuple1,tuple2))
In [7]: dict2Out[7]: {'yang': 100, 'li': 99, 'wang': 98}需要注意的是,字典的索引通常是不可變的,即不可被更改,比如:整數,字符串,元組等。這被稱為「可哈希性」。如果用可變的數據(比如列表)作為索引,會引發錯誤:
In [8]: dict3 = {[1,2]:1}TypeError Traceback (most recent call last)<ipython-input-8-d6261989d799> in <module>()----> 1 dict3 = {[1,2]:1}
TypeError: unhashable type: 'list'可以使用hash函數檢測一個元素是不是可哈希的(可以被用作字典的索引):
In [9]: hash(1)Out[9]: 1
In [10]: hash('stat')Out[10]: -5413363681840078896
In [11]: hash([1,2])TypeError Traceback (most recent call last)<ipython-input-11-9ce67481a686> in <module>()----> 1 hash([1,2])
TypeError: unhashable type: 'list'可以直接利用dict[]的方法進行操作,只不過方括號裡面輸入的是字典的索引,就可以不是數字了:
In [12]: dict1Out[12]: {'a': 'stat', 5: [1, 2, 3, 4]}
In [13]: dict1[7] = 'an integer'
In [14]: dict1Out[14]: {'a': 'stat', 5: [1, 2, 3, 4], 7: 'an integer'}
In [15]: dict1['a']Out[15]: 'stat'同時,還可以用檢查列表和元組是否包含某個值的辦法,來檢查字典中是否含有某個鍵:
In [17]: 'a' in dict1Out[17]: True
In [18]: 'an integer' in dict1Out[18]: False刪除元素可以使用del關鍵字以及pop函數刪除索引及其對應的元素:In [19]: dict1[8] = (1,1)
In [20]: dict1[9] = (2,2)
In [21]: dict1Out[21]: {'a': 'stat', 5: [1, 2, 3, 4], 7: 'an integer', 8: (1, 1), 9: (2, 2)}
In [22]: del dict1[8]
In [23]: dict1Out[23]: {'a': 'stat', 5: [1, 2, 3, 4], 7: 'an integer', 9: (2, 2)}
In [24]: pop9 = dict1.pop(9)
In [25]: pop9Out[25]: (2, 2)
In [26]: dict1Out[26]: {'a': 'stat', 5: [1, 2, 3, 4], 7: 'an integer'}在這裡我們在dict1裡面生成了索引為8和9的兩個元素,然後利用del刪除8對應的元素,再利用pop函數刪除了9對應的元素。
利用keys()函數可以輸出字典的索引,利用values()輸出字典的值,這兩種方法輸出的順序是一樣的。這兩個函數都是生成器,所以我們還是需要先轉化成別的數據結構,才可以看到結果。
In [27]: dict1Out[27]: {'a': 'stat', 5: [1, 2, 3, 4], 7: 'an integer'}
In [28]: dict1.keys()Out[28]: dict_keys(['a', 5, 7])
In [29]: type(dict1.keys())Out[29]: dict_keys
In [30]: list(dict1.keys())Out[30]: ['a', 5, 7]
In [31]: list(dict1.values())Out[31]: ['stat', [1, 2, 3, 4], 'an integer']合併字典利用update()函數可以將一個字典與另一個字典合併:In [32]: dict1Out[32]: {'a': 'stat', 5: [1, 2, 3, 4], 7: 'an integer'}
In [33]: dict2Out[33]: {'yang': 100, 'li': 99, 'wang': 98}
In [34]: dict1.update(dict2)
In [35]: dict1Out[35]:{'a': 'stat', 5: [1, 2, 3, 4], 7: 'an integer', 'yang': 100, 'li': 99, 'wang': 98}
In [36]: dict2Out[36]: {'yang': 100, 'li': 99, 'wang': 98}可以看到,字典dict1發生了變化,而dict2並沒有變化。
4.2 集合這裡的集合與數學上的集合類似,都是由無序的不可重複的元素組合起來的。和前面的數據結構不同的是,集合裡面沒有索引。下面介紹一些集合的操作:
使用大括號或者是set函數來生成集合:
In [37]: set1 = {1,2,3}
In [38]: set1Out[38]: {1, 2, 3}
In [39]: set2 = set([3,4,5,6])
In [40]: set2Out[40]: {3, 4, 5, 6}需要注意的是,與字典類似,集合的元素通常是不可變的。所以如果要把列表作為一個元素放在集合裡面,必須要先轉換成元組:
In [42]: set3 = {[1,2,3,4,5]}TypeError Traceback (most recent call last)<ipython-input-42-95e8c6514767> in <module>()----> 1 set3 = {[1,2,3,4,5]}
TypeError: unhashable type: 'list'
In [43]: set3 = {tuple([1,2,3,4,5])}
In [44]: set3Out[44]: {(1, 2, 3, 4, 5)}Python支持很多的數學集合運算。下面介紹一些集合的運算。請參見下面的圖表:
集合運算需要注意的是,通過函數以及替代語法都可以實現相同的功能,比如對於update函數:
In [63]: a = {1,2,3,4,5}
In [64]: b = {3,4,5,6,7,8}
In [65]: c = a
In [66]: cOut[66]: {1, 2, 3, 4, 5}
In [67]: a.update(b)
In [68]: aOut[68]: {1, 2, 3, 4, 5, 6, 7, 8}
In [69]: c |= b
In [70]: cOut[70]: {1, 2, 3, 4, 5, 6, 7, 8}