- Python 支援多種資料結構,包含清單(list)、集合(set)、字典(dict)、Tuple !
- 每一種資料結構有其使用方式,請注意其不同的使用條件!
- 清單(list):其使用特性為有序、具備索引值、內容長度可變動,使用 [] 符號,內容使用逗號區隔!
-
範例 1:
#python3 Python 3.8.3 (default, Aug 31 2020, 16:03:14) [GCC 8.3.1 20191121 (Red Hat 8.3.1-5)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> names = ['Peter', 'James', 'Mary'] >>> names ['Peter', 'James', 'Mary'] >>> print(names) ['Peter', 'James', 'Mary'] >>> print(names[0]) Peter
-
範例 1:
- 可使用 append()、extend()、pop()、remove()、reverse()、sort() 等方法,進行資料結構的調整!
-
範例 2:
>>> names = ['Peter', 'James', 'Mary'] >>> names ['Peter', 'James', 'Mary', 'D', 'a', 'v', 'i', 'd'] >>> names.remove('D') >>> names ['Peter', 'James', 'Mary', 'a', 'v', 'i', 'd'] >>> names.remove(names[3]) >>> names ['Peter', 'James', 'Mary', 'v', 'i', 'd'] >>> names.remove(names[3-4]) >>> names ['Peter', 'James', 'Mary', 'v', 'i'] (註:因為 3-4 = -1,所以是由後往前數一個欄位) >>> names.pop(1) 'James' >>> names ['Peter', 'Mary', 'v', 'i'] (註:pop() 是指要提出哪一個欄位的值!提出後,不寫回原欄位!) >>> names.sort() >>> names ['Mary', 'Peter', 'i', 'v'] >>> names.append('James') >>> names ['Mary', 'Peter', 'i', 'v', 'James'] >>> names.reverse() >>> names ['James', 'v', 'i', 'Peter', 'Mary'] >>> names.extend(['David','Poll']) >>> names ['James', 'v', 'i', 'Peter', 'Mary', 'David', 'Poll']
-
範例 2:
- 集合(set):其特色為內容是無序但不重複!使用{}符號包含元素,元素間使用逗號分隔!
-
範例 1:
#python3 Python 3.8.3 (default, Aug 31 2020, 16:03:14) [GCC 8.3.1 20191121 (Red Hat 8.3.1-5)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> >>> names = set() >>> names set() >>> names.add('Peter') >>> names {'Peter'} >>> names = {'Peter','Mary'} >>> names {'Mary', 'Peter'} (註:集合會自動排序) >>> names.add('Peter') >>> names {'Mary', 'Peter'} >>> names.remove('Peter') >>> names {'Mary'} >>> 'Peter' in names False
-
範例 1:
- 字典(dict):成雙成對的 key-value 架構!
-
範例 1:
#python3 Python 3.8.3 (default, Aug 31 2020, 16:03:14) [GCC 8.3.1 20191121 (Red Hat 8.3.1-5)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> >>> userpws = {'Peter':32123 , 'David':'abc123'} >>> userpws['David'] 'abc123' >>> userpws['Peter'] 32123 >>> userpws['Mary'] = 987654 >>> userpws {'Peter': 32123, 'David': 'abc123', 'Mary': 987654} >>> del userpws['David'] >>> userpws {'Peter': 32123, 'Mary': 987654} >>> 'Mary' in userpws True >>> userpws['Mary'] = 12345 >>> userpws {'Peter': 32123, 'Mary': 12345} >>> userpws.get('David') >>> userpws.get('David') == None True >>> userpws.get('David', 'ABC123') 'ABC123' >>> userpws {'Peter': 32123, 'Mary': 12345} (註:get 只是暫存資料!) >>> list(userpws.items()) [('Peter', 32123), ('Mary', 12345)] >>> list(userpws.keys()) ['Peter', 'Mary'] >>> list(userpws.values()) [32123, 12345]
-
範例 1:
- Tuple(tuple):有序的資料結構,使用[]指定索引元素!建立之後,就無法變動!
-
範例 1:
#python3 Python 3.8.3 (default, Aug 31 2020, 16:03:14) [GCC 8.3.1 20191121 (Red Hat 8.3.1-5)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> >>> 10, (10,) >>> 10,20,30 (10, 20, 30) >>> acct = 1, 'Peter', True >>> acct (1, 'Peter', True) >>> type(acct) <class 'tuple'> >>> print(acct[1]) Peter >>> acct[1] = "Mary" Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'tuple' object does not support item assignment >>> id, names, access = acct >>> names 'Peter' >>> access, names, id = id, names, access >>> access 1 (註:用來置換變數值內容) >>> a, *b, c = [1,2,3,4,5] >>> b [2, 3, 4]
-
範例 1: