周楷翔 @ 資芽 2022 北區 Py 班
參考自 2020 北區 by robert1003 & 2021 竹區 by Sylveon
(費波那契簡報)
還記得那是什麼齁
primes = [2, 3, 5, 7, 10] primes[4] = 11 print(primes[0])
如果我想登記大家的成績,就把學號當成 index
score = list() score[10712345] = 87 # 該怎麼避免 IndexError?
(不要這麼做,拜託)
可是敝校的學號有英文字母…
score = list() score["B07705022"] = 87 # TypeError: list indices must be integers or slices, not str
真的不行了…
除了轉學以外,有其他方法嗎?
這就是為什麼我們需要 Dictionary!
什麼是 Dictionary
大括號代表 dict
score = {} # or score = dict()
預設內容用 :
區隔 key 與 value(i.e., key: value
)
score = {"Alice": 36, "Bob": 37}
稍微複習一下
my_list = [] my_tuple = () my_dict = {}
score = {"Alice": 36, "Bob": 37} print(score["Alice"]) print(score["Bob"]) print(score["Carol"]) # 等等,這不存在啊?
36
37
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-1-34eaef53f35c> in <module>()
2 print(score["Alice"])
3 print(score["Bob"])
----> 4 print(score["Carol"])
KeyError: 'Carol'
有沒有方法避免噴出 KeyError 呢?
if "Carol" in score: # 不會進去這個 if print(score["Carol"]) print(score.get("Carol")) # 回傳 None print(score.get("Carol", 0)) # 回傳 0
get(key, default value)
:
新增:直接用 =
賦值即可
score["Carol"] = 100 print(score) # {'Alice': 36, 'Bob': 37, 'Carol': 100}
修改:直接用 =
賦值即可
score["Alice"] = 80 print(score) # {'Alice': 80, 'Bob': 37, 'Carol': 100}
恩對,兩個長得一模一樣
observations
裡面,每個字元分別出現幾次?
observations = ["a", "b", "c", "d", "a", "e", "c", "f", "f", "c"]
P.S. 輸出是
{'a': 2, 'b': 1, 'c': 3, 'd': 1, 'e': 1, 'f': 2}
方法一:for key in dict.keys
for k in score.keys(): print(k)
遍歷的是 key
Alice
Bob
Carol
方法二:for value in dict.values()
for v in score.values(): print(v)
遍歷的是 value
80
37
100
方法三:for key, value in dict.items()
for k, v in score.items(): print(k, v)
key 跟 value 同時遍歷,以 (key, value)
的 tuple 表示
Alice 80
Bob 37
Carol 100
方法一:pop
score.pop("Alice") # 可以注意一下這回傳什麼 print(score) # {'Bob': 37, 'Carol': 100}
刪除指定的 key(以及他對應到的 value)
方法二:del
del score["Bob"] # 注意一下,這有回傳值嗎? print(score) # {'Carol': 100}
方法三:clear
放大絕,全部清空!
score.clear() print(score) # {}
聽不懂就算了
並不是什麼東西都能當 key
score[[1, 2, 3]] = 456
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'
原因有點複雜(關鍵字:immutable),總之拿 int, float, str, tuple 可以當 key 不會遇到這些問題
Dictionary 是沒有排序的
dict1 = {"Alice": 100, "Bob": 87} dict2 = {"Bob": 87, "Alice": 100} print(dict1) # {'Alice': 100, 'Bob': 87} print(dict2) # {'Bob': 87, 'Alice': 100} print(dict1 == dict2) # True
(從 Python 3.7 起,Dictionary 的順序保證會是插入順序)
Dictionary 有許多不同的名字:associative array, map, symbol table
不同語言可能會以不同的名字提供相同的功能
e.g. C++ 的 <map>