字典簡介
- 字典在 Python 裡面是非常重要的資料型別,而且很常用
- 字典是以關鍵字(鍵)為索引,關鍵字(鍵)可以是任意不可變型別
- 字典由鍵和對應值成對組成,字典中所有的鍵值對放在 { } 中間,每一對鍵值之間用逗號分開
字典栗子
var = {'a': 'A', 'b': 'B', 'c': 'C'}
字典中包含 3 個鍵值對
- 鍵 ‘a’ 的值是 ‘A’
- 鍵 ‘b’ 的值是 ‘B’
- 鍵 ‘c’ 的值是 ‘C’
字典通常用於描述物件的各種屬性
例如一本書,有書名、作者名、出版社等各種屬性,可以使用字典描述如下
book = {
'title': 'Python 入門基礎',
'author': '張三',
'press': '機械工業出版社'
}
鍵的取值規則
- 可以是任意不可變型別
- 通常是字串或數字
- 如果一個元組只包含字串、數字或元組,那麼這個元組也可以用作鍵
- 但如果元組直接或間接地包含了可變物件,那麼它就不能用作鍵
- 列表不能用作鍵
宣告一個空字典
# 空字典
dict_ = {}
print(dict_,type(dict_)) dict_ = dict()
print(dict_,type(dict_)) # 輸出結果
{} <class 'dict'>
{} <class 'dict'>
讀取字典
兩種方式
- 通過索引 [ key ]
- 通過 .get(key) 方法
book = {
'title': 'Python 入門基礎',
'author': '張三',
'press': '機械工業出版社'
} print(book["title"])
print(book["author"])
print(book.get("press"))
print(book.get("a"))
兩種方式有啥區別?
- .get(key) 當 key 不存在的時候會返回 None
- [ key ] 如果 key 不存在則會報錯 KeyError: 'authors'
修改字典
兩種方式
- 通過索引 [ key ] 賦值
- 通過 .update() 方法
# 修改字典
book = {
'title': 'Python 入門基礎',
'author': '張三',
'press': '機械工業出版社'
} book["author"] = "小菠蘿"
print(book) book.update({"title": "新書"})
print(book) # 輸出結果
{'title': 'Python 入門基礎', 'author': '小菠蘿', 'press': '機械工業出版社'}
{'title': '新書', 'author': '小菠蘿', 'press': '機械工業出版社'}
在字典中增加鍵值對
# 新加鍵值對
book = {
'title': 'Python 入門基礎',
'author': '張三',
'press': '機械工業出版社'
}
book["money"] = 22
print(book) # 輸出結果
{'title': 'Python 入門基礎', 'author': '張三', 'press': '機械工業出版社', 'money': 22}
在字典中刪除鍵值對
兩種方式
- 通過關鍵字 del
- 通過 .pop(key)
# 刪除鍵值對
book = {
'title': 'Python 入門基礎',
'author': '張三',
'press': '機械工業出版社'
} del book["press"]
print(book) print(book.pop("author"))
print(book) # 輸出結果
{'title': 'Python 入門基礎', 'author': '張三'}
張三
{'title': 'Python 入門基礎'}
查詢字典
通過關鍵字 in 檢查字典中是否包含指定鍵
# in
book = {
'title': 'Python 入門基礎',
'author': '張三',
'press': '機械工業出版社'
} print("title" in book)
print("titles" in book)
print("張三" in book) # 輸出結果
True
False
False
常見函式
len
獲取字典中鍵值對的數量
# len
var = {'a': 'A', 'b': 'B', 'c': 'C'}
print(len(var)) # 輸出結果
3
list
返回包含該字典中所有鍵的列表
# list
book = {
'title': 'Python 入門基礎',
'author': '張三',
'press': '機械工業出版社'
}
print(list(book)) # 輸出結果
['title', 'author', 'press']
- 這個返回結果是無序的
- tuple() 一樣也可以這樣哦,返回的是鍵組成的元組
sorted
返回包含該字典中所有鍵的有序列表
# sorted
book = {
'title': 'Python 入門基礎',
'author': '張三',
'press': '機械工業出版社'
}
print(sorted(book)) # 輸出結果
['author', 'press', 'title']
dict() 建立字典物件詳解
重點
dict 是一個類,而不是函式
原始碼
從原始碼註釋來看,dict 有四種傳參方式,下面將一一舉例
建立空字典
# 建立空字典
dict1 = dict()
print(dict1) # 輸出結果
{}
通過 iterable 建立字典
# 傳遞 list 來建立字典
mid = [("a", 1), ("b", 2)]
dict2 = dict(mid)
print(dict2) # 輸出結果
{'a': 1, 'b': 2}
iterable 的詳解:https://www.cnblogs.com/poloyy/p/14658433.html
通過關鍵字引數建立字典
# 傳遞關鍵字引數來建立字典
dict3 = dict(name="yy", age=24)
print(dict3) # 輸出結果
{'name': 'yy', 'age': 24}
通過另一個字典物件建立字典
mid = {"title": [1, 2, 3]}
# 相當於淺拷貝
dict4 = dict(mid) print(dict4)
print(id(mid), id(dict4)) mid["name"] = "test"
mid["title"][1] = 1 print(mid, dict4) # 輸出結果
{'title': [1, 2, 3]}
4498981760 4500413824
{'title': [1, 1, 3], 'name': 'test'} {'title': [1, 1, 3]}
這種傳參方式相當於淺拷貝,新的字典物件和舊的字典物件並不指向同一個物件引用
假設直接用賦值的方式,因為字典是可變物件,所以新舊字典物件都會指向同一個物件引用
dict1 = {1: 1}
dict2 = dict1
print(id(dict1), id(dict2)) # 輸出結果
4355281792 4355281792
所以,當想複製一個新的字典物件但是又不想指向同一個物件引用的話,最好還是使用 dict() 的方式
字典常見方法
https://www.cnblogs.com/poloyy/p/15084264.html
遍歷字典的多種方式
https://www.cnblogs.com/poloyy/p/15084302.html