溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Python的字典是什么及怎么創(chuàng)建

發(fā)布時(shí)間:2023-04-17 11:29:52 來源:億速云 閱讀:185 作者:iii 欄目:開發(fā)技術(shù)

這篇“Python的字典是什么及怎么創(chuàng)建”文章的知識(shí)點(diǎn)大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細(xì),步驟清晰,具有一定的借鑒價(jià)值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“Python的字典是什么及怎么創(chuàng)建”文章吧。

字典(Dictionary)

字典是一個(gè)無(wú)序、可變和有索引的集合。在 Python 中,字典用花括號(hào)編寫,擁有鍵和值。

實(shí)例

創(chuàng)建并打印字典:

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

運(yùn)行實(shí)例

Python的字典是什么及怎么創(chuàng)建

訪問項(xiàng)目

您可以通過在方括號(hào)內(nèi)引用其鍵名來訪問字典的項(xiàng)目:

實(shí)例

獲取 “model” 鍵的值:

x = thisdict["model"]

運(yùn)行實(shí)例

Python的字典是什么及怎么創(chuàng)建

還有一個(gè)名為 get() 的方法會(huì)給你相同的結(jié)果:

實(shí)例

獲取 “model” 鍵的值:

x = thisdict.get("model")

運(yùn)行實(shí)例

Python的字典是什么及怎么創(chuàng)建

更改值

您可以通過引用其鍵名來更改特定項(xiàng)的值:

實(shí)例

把 “year” 改為 2019:

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

運(yùn)行實(shí)例

Python的字典是什么及怎么創(chuàng)建

遍歷字典

您可以使用 for 循環(huán)遍歷字典。

循環(huán)遍歷字典時(shí),返回值是字典的鍵,但也有返回值的方法。

實(shí)例

逐個(gè)打印字典中的所有鍵名:

for x in thisdict:
  print(x)

運(yùn)行實(shí)例

Python的字典是什么及怎么創(chuàng)建

實(shí)例

逐個(gè)打印字典中的所有值:

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

運(yùn)行實(shí)例

Python的字典是什么及怎么創(chuàng)建

實(shí)例

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

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

運(yùn)行實(shí)例

Python的字典是什么及怎么創(chuàng)建

實(shí)例

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

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

運(yùn)行實(shí)例

Python的字典是什么及怎么創(chuàng)建

檢查鍵是否存在

要確定字典中是否存在指定的鍵,請(qǐng)使用 in 關(guān)鍵字:

實(shí)例

檢查字典中是否存在 “model”:

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

運(yùn)行實(shí)例

Python的字典是什么及怎么創(chuàng)建

字典長(zhǎng)度

要確定字典有多少項(xiàng)目(鍵值對(duì)),請(qǐng)使用 len() 方法。

實(shí)例

打印字典中的項(xiàng)目數(shù):

print(len(thisdict))

運(yùn)行實(shí)例

Python的字典是什么及怎么創(chuàng)建

添加項(xiàng)目

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

實(shí)例

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

運(yùn)行實(shí)例

Python的字典是什么及怎么創(chuàng)建

刪除項(xiàng)目

有幾種方法可以從字典中刪除項(xiàng)目:

實(shí)例

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

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

運(yùn)行實(shí)例

Python的字典是什么及怎么創(chuàng)建

實(shí)例

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

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

運(yùn)行實(shí)例

Python的字典是什么及怎么創(chuàng)建

實(shí)例

del 關(guān)鍵字刪除具有指定鍵名的項(xiàng)目:

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

運(yùn)行實(shí)例

Python的字典是什么及怎么創(chuàng)建

實(shí)例

del 關(guān)鍵字也可以完全刪除字典:

thisdict =	{
  "brand": "Porsche",
  "model": "911",
  "year": 1963
}
del thisdict

print(thisdict) #this 會(huì)導(dǎo)致錯(cuò)誤,因?yàn)?nbsp;"thisdict" 不再存在。

運(yùn)行實(shí)例

Python的字典是什么及怎么創(chuàng)建

實(shí)例

clear() 關(guān)鍵字清空字典:

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

運(yùn)行實(shí)例

Python的字典是什么及怎么創(chuàng)建

復(fù)制字典

您不能通過鍵入 dict2 = dict1 來復(fù)制字典,因?yàn)椋篸ict2 只是對(duì) dict1 的引用,而 dict1 中的更改也將自動(dòng)在 dict2 中進(jìn)行。

有一些方法可以進(jìn)行復(fù)制,一種方法是使用內(nèi)建的字典方法 copy()。

實(shí)例

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

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

運(yùn)行實(shí)例

Python的字典是什么及怎么創(chuàng)建

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

實(shí)例

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

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

運(yùn)行實(shí)例

Python的字典是什么及怎么創(chuàng)建

嵌套字典

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

實(shí)例

創(chuàng)建包含三個(gè)字典的字典:

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

運(yùn)行實(shí)例

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

或者,如果您想嵌套三個(gè)已經(jīng)作為字典存在的字典:

實(shí)例

創(chuàng)建三個(gè)字典,然后創(chuàng)建一個(gè)包含其他三個(gè)字典的字典:

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
}

運(yùn)行實(shí)例

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

dict() 構(gòu)造函數(shù)

也可以使用 dict() 構(gòu)造函數(shù)創(chuàng)建新的字典:

實(shí)例

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

運(yùn)行實(shí)例

Python的字典是什么及怎么創(chuàng)建

字典方法

Python 提供一組可以在字典上使用的內(nèi)建方法。

Python的字典是什么及怎么創(chuàng)建

以上就是關(guān)于“Python的字典是什么及怎么創(chuàng)建”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對(duì)大家有幫助,若想了解更多相關(guān)的知識(shí)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI