Python Training Course III
Data Structure Type
Lists
Tuples 發音 too-pull 或 tub-pull 都對
Dictionaries
Sets
2
Lists and Tuples
a sequence of items indexed by their integer position
there are two sequence structures: tuples and lists
contain zero or more elements
the elements can be of different types
tuples are immutable
lists are mutable
3
Lists
4
如何建立 list
Create with [ ] or list()
Value 可以不必是唯一
5
如何建立 list
Convert Other Data Types to Lists
Create list with split
6
Get an Item by Using [ offset ]
7
Lists of Lists
8
Change an Item by [ offset ], get slice of lists
9
Add an Item to the End with append()
10
Combine Lists by Using extend() or +=
11
Add an Item by Offset with insert()
12
Delete an Item by Offset with del
del is a Python statement, not a list method—you don’t say marxes[-2].del() .
It’s sort of the reverse of assignment ( = ): it detaches a name from a Python
object and can free up the object’s memory if that name was the last
reference to it.
13
Delete an Item by Value with remove()
14
Get an Item by Offset and Delete It by Using pop()
實現 LIFO (stack) : append(), pop()
實現 FIFO (queue) : append(), pop(0)
15
Find an Item’s Offset by Value with index()
16
Test for a Value with in
如果經常要檢查 list 中的值是否存在 ?
但不在乎值的位置時,使用另一種
資料結構 set 會更好些
17
Count Occurrences of a Value by Using count()
18
Convert to a String with join()
join() 和 split() 剛好是相反的功能
19
Reorder Items with sort()
sort() sorts the list itself
sorted() returns a sorted copy of the list
20
Get Length by Using len()
21
Assign with =, Copy with copy()
22
copy a list to an independent, fresh list
• The list copy() function
• The list() conversion function
• The list slice [:]
23
Lists 練習題
Book P.67
Things to Do 3.1~3.9
24
Tuple
25
Create a Tuple by Using ()
26
Using tuple exchange values without temp. variable
Tuple conversion function
27
Tuple 與 Lists 的比較
tuples 使用較少的記憶體空間
不會因失誤而損壞 tuple
可將 tuples 作為 dictionary 的 keys
函數的引數是以 tuples 傳遞
28
Dictionary
29
Dictionary
和 list 類似,但並在意順序而是以唯一的 key 來存取 value
key 通常是字串,但不限於,只要是不可改變的類型 ( boolean,
integer, float, tuple, string, and…)
Dictionaries are mutable, so you can add, delete, and change
their key-value elements.
簡稱 dict
30
Dictionary Create with { }
list, tuple 或 dictionary
可以在最後一項有逗點
31
Convert by Using dict()
32
Add or Change an Item by [ key ]
33
Key 必須是唯一
34
Combine Dictionaries with update()
35
Delete an Item by Key with del
Delete All Items by Using clear()
or reassign { }
36
Test for a Key by Using in
37
Get an Item by [ key ]
38
Get All Keys by Using keys()
Get All Values by Using values()
Get All Key-Value Pairs by Using items()
39
Assign with =, Copy with copy()
Dictionaries 練習題
Book P.67 ~ P.68
Things to Do 3.10~3.14
41
Set
set 可以看成是 “有 key 無 value” 的 dictionary
42
回顧一下小學教過的集合 (sets) 概念
Create with set()
{ } 會產生一個空的 dict ,
而不是空的 set
Convert from Other Data Types with set()
Test for Value by Using in
Combinations and Operators
交集
/home/jianlong/ 影片 /Lady.Bird.mp4
Combinations and Operators
Combinations and Operators
Make Bigger Data Structures
About key of dict
Dictionary keys need to be immutable
a list, dictionary, or set can’t be a key for another dictionary
a tuple can be
Bigger Data Structures 練習題
Book P.68
Things to Do 3.15~3.18
52