亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Python入門(mén)教程(十五)Python的字典

 更新時(shí)間:2023年04月17日 10:52:27   作者:輕松學(xué)Python  
這篇文章主要介紹了Python入門(mén)教程(十五)Python的字典,Python是一門(mén)非常強(qiáng)大好用的語(yǔ)言,也有著易上手的特性,本文為入門(mén)教程,需要的朋友可以參考下

字典(Dictionary)

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

實(shí)例

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

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

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

訪(fǎng)問(wèn)項(xiàng)目

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

實(shí)例

獲取 “model” 鍵的值:

x = thisdict["model"]

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

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

實(shí)例

獲取 “model” 鍵的值:

x = thisdict.get("model")

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

更改值

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

實(shí)例

把 “year” 改為 2019:

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

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

遍歷字典

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

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

實(shí)例

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

for x in thisdict:
  print(x)

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

實(shí)例

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

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

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

實(shí)例

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

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

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

實(shí)例

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

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

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

檢查鍵是否存在

要確定字典中是否存在指定的鍵,請(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í)例

字典長(zhǎng)度

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

實(shí)例

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

print(len(thisdict))

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

添加項(xiàng)目

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

實(shí)例

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

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

刪除項(xiàng)目

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

實(shí)例

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

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

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

實(shí)例

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

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

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

實(shí)例

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

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

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

實(shí)例

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

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

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

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

實(shí)例

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

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

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

復(fù)制字典

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

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

實(shí)例

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

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

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

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

實(shí)例

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

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

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

嵌套字典

詞典也可以包含許多詞典,這被稱(chē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)來(lái)賦值
print(thisdict)

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

字典方法

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

到此這篇關(guān)于Python入門(mén)教程(十五)Python的字典的文章就介紹到這了,更多相關(guān)Python 字典內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • python繪圖時(shí),坐標(biāo)軸負(fù)號(hào)顯示不出來(lái)的解決

    python繪圖時(shí),坐標(biāo)軸負(fù)號(hào)顯示不出來(lái)的解決

    這篇文章主要介紹了python繪圖時(shí),坐標(biāo)軸負(fù)號(hào)顯示不出來(lái)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • Python實(shí)現(xiàn)郵件發(fā)送功能的示例詳解

    Python實(shí)現(xiàn)郵件發(fā)送功能的示例詳解

    Python對(duì)SMTP支持有smtplib和email兩個(gè)模塊,email負(fù)責(zé)構(gòu)造郵件,smtplib負(fù)責(zé)發(fā)送郵件。本文將以qq郵箱為例,實(shí)現(xiàn)自己給自己發(fā)送郵件的功能,感興趣的可以了解一下
    2022-11-11
  • 如何使用Typora+MinIO+Python代碼打造舒適協(xié)作環(huán)境

    如何使用Typora+MinIO+Python代碼打造舒適協(xié)作環(huán)境

    這篇文章主要介紹了如何使用Typora+MinIO+Python代碼打造舒適協(xié)作環(huán)境,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-05-05
  • Python使用django獲取用戶(hù)IP地址的方法

    Python使用django獲取用戶(hù)IP地址的方法

    這篇文章主要介紹了Python使用django獲取用戶(hù)IP地址的方法,實(shí)例分析了django獲取用戶(hù)IP地址過(guò)程中出現(xiàn)的問(wèn)題與對(duì)應(yīng)的解決方法,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下
    2015-05-05
  • opencv之顏色過(guò)濾只留下圖片中的紅色區(qū)域操作

    opencv之顏色過(guò)濾只留下圖片中的紅色區(qū)域操作

    這篇文章主要介紹了opencv之顏色過(guò)濾只留下圖片中的紅色區(qū)域操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-06-06
  • Python 閉包的使用方法

    Python 閉包的使用方法

    這篇文章主要介紹了Python 閉包的使用方法的相關(guān)資料,了解閉包及定義方法和使用,需要的朋友可以參考下
    2017-09-09
  • Python?pandas中apply函數(shù)簡(jiǎn)介以及用法詳解

    Python?pandas中apply函數(shù)簡(jiǎn)介以及用法詳解

    apply()函數(shù)是pandas里面所有函數(shù)中自由度最高的函數(shù), apply()函數(shù)的參數(shù)是一個(gè)函數(shù)指針,這里可以使用lambda表達(dá)式幫助簡(jiǎn)化代碼,下面這篇文章主要給大家介紹了關(guān)于Python?pandas中apply函數(shù)簡(jiǎn)介以及用法的相關(guān)資料,需要的朋友可以參考下
    2022-09-09
  • 最新評(píng)論