Python 字典

字典(Dictionary)

字典是一個無序、可變和有索引的集合。在 Python 中,字典用花括號編寫,擁有鍵和值。

實例

創建并打印字典:

thisdict =	{
  "brand": "Porsche",
  "model": "911",
  "year": 1963
}
print(thisdict)

運行實例

訪問項目

您可以通過在方括號內引用其鍵名來訪問字典的項目:

實例

獲取 "model" 鍵的值:

x = thisdict["model"]

運行實例

還有一個名為 get() 的方法會給你相同的結果:

實例

獲取 "model" 鍵的值:

x = thisdict.get("model")

運行實例

更改值

您可以通過引用其鍵名來更改特定項的值:

實例

把 "year" 改為 2019:

thisdict =	{
  "brand": "Porsche",
  "model": "911",
  "year": 1963
}
thisdict["year"] = 2019

運行實例

遍歷字典

您可以使用 for 循環遍歷字典。

循環遍歷字典時,返回值是字典的鍵,但也有返回值的方法。

實例

逐個打印字典中的所有鍵名:

for x in thisdict:
  print(x)

運行實例

實例

逐個打印字典中的所有值:

for x in thisdict:
  print(thisdict[x])

運行實例

實例

您還可以使用 values() 函數返回字典的值:

for x in thisdict.values():
  print(x)

運行實例

實例

通過使用 items() 函數遍歷鍵和值:

for x, y in thisdict.items():
  print(x, y)

運行實例

檢查鍵是否存在

要確定字典中是否存在指定的鍵,請使用 in 關鍵字:

實例

檢查字典中是否存在 "model":

thisdict =	{
  "brand": "Porsche",
  "model": "911",
  "year": 1963
}
if "model" in thisdict:
  print("Yes, 'model' is one of the keys in the thisdict dictionary")

運行實例

字典長度

要確定字典有多少項目(鍵值對),請使用 len() 方法。

實例

打印字典中的項目數:

print(len(thisdict))

運行實例

添加項目

通過使用新的索引鍵并為其賦值,可以將項目添加到字典中:

實例

thisdict =	{
  "brand": "Porsche",
  "model": "911",
  "year": 1963
}
thisdict["color"] = "red"
print(thisdict)

運行實例

刪除項目

有幾種方法可以從字典中刪除項目:

實例

pop() 方法刪除具有指定鍵名的項:

thisdict =	{
  "brand": "Porsche",
  "model": "911",
  "year": 1963
}
thisdict.pop("model")
print(thisdict)

運行實例

實例

popitem() 方法刪除最后插入的項目(在 3.7 之前的版本中,刪除隨機項目):

thisdict =	{
  "brand": "Porsche",
  "model": "911",
  "year": 1963
}
thisdict.popitem()
print(thisdict)

運行實例

實例

del 關鍵字刪除具有指定鍵名的項目:

thisdict =	{
  "brand": "Porsche",
  "model": "911",
  "year": 1963
}
del thisdict["model"]
print(thisdict)

運行實例

實例

del 關鍵字也可以完全刪除字典:

thisdict =	{
  "brand": "Porsche",
  "model": "911",
  "year": 1963
}
del thisdict
print(thisdict) #this 會導致錯誤,因為 "thisdict" 不再存在。

運行實例

實例

clear() 關鍵字清空字典:

thisdict =	{
  "brand": "Porsche",
  "model": "911",
  "year": 1963
}
thisdict.clear()
print(thisdict)

運行實例

復制字典

您不能通過鍵入 dict2 = dict1 來復制字典,因為:dict2 只是對 dict1 的引用,而 dict1 中的更改也將自動在 dict2 中進行。

有一些方法可以進行復制,一種方法是使用內建的字典方法 copy()

實例

使用 copy() 方法來復制字典:

thisdict =	{
  "brand": "Porsche",
  "model": "911",
  "year": 1963
}
mydict = thisdict.copy()
print(mydict)

運行實例

制作副本的另一種方法是使用內建方法 dict()

實例

使用 dict() 方法創建字典的副本:

thisdict =	{
  "brand": "Porsche",
  "model": "911",
  "year": 1963
}
mydict = dict(thisdict)
print(mydict)

運行實例

嵌套字典

詞典也可以包含許多詞典,這被稱為嵌套詞典。

實例

創建包含三個字典的字典:

myfamily = {
  "child1" : {
    "name" : "Phoebe Adele",
    "year" : 2002
  },
  "child2" : {
    "name" : "Jennifer Katharine",
    "year" : 1996
  },
  "child3" : {
    "name" : "Rory John",
    "year" : 1999
  }
}

運行實例

或者,如果您想嵌套三個已經作為字典存在的字典:

實例

創建三個字典,然后創建一個包含其他三個字典的字典:

child1 = {
  "name" : "Phoebe Adele",
  "year" : 2002
}
child2 = {
  "name" : "Jennifer Katharine",
  "year" : 1996
}
child3 = {
  "name" : "Rory John",
  "year" : 1999
}
myfamily = {
  "child1" : child1,
  "child2" : child2,
  "child3" : child3
}

運行實例

dict() 構造函數

也可以使用 dict() 構造函數創建新的字典:

實例

thisdict = dict(brand="Porsche", model="911", year=1963)
# 請注意,關鍵字不是字符串字面量
# 請注意,使用了等號而不是冒號來賦值
print(thisdict)

運行實例

字典方法

Python 提供一組可以在字典上使用的內建方法。

方法 描述
clear() 刪除字典中的所有元素
copy() 返回字典的副本
fromkeys() 返回擁有指定鍵和值的字典
get() 返回指定鍵的值
items() 返回包含每個鍵值對的元組的列表
keys() 返回包含字典鍵的列表
pop() 刪除擁有指定鍵的元素
popitem() 刪除最后插入的鍵值對
setdefault() 返回指定鍵的值。如果該鍵不存在,則插入具有指定值的鍵。
update() 使用指定的鍵值對字典進行更新
values() 返回字典中所有值的列表