Python四大金剛之字典詳解
引言

列表、字典:可變序列,可以執(zhí)行增刪改排序等
字典:無(wú)序的


一、字典的創(chuàng)建
#使用{}創(chuàng)建
scores = {'張三':100 ,'李四':98 ,'王麻子':72}
print(scores)
print(type(scores))
#使用內(nèi)置函數(shù)dict()
student = dict(name = 'jack ', age = 16)
print(student)
print(type(student))
二、字典元素的操作
(一)獲取

#獲取字典中的元素
#方法一:
print(scores['張三'])
#方法二:
print(scores.get('張三'))
print(scores.get('66'))
#如果查找的不存在,返回none
(二)增刪改

刪除操作
del scores['張三'] #根據(jù)索引刪除 key 和value print(scores) scores.clear() #刪除所有 print(scores)
新增操作 (直接增加)
scores['趙四'] = 80
三、獲取字典的視圖

# 獲取所有key值 key = scores.keys() print(key) print(type(key)) print(list(key)) #將key組成的視圖轉(zhuǎn)成list #獲取所有value值 value = scores.values() print(value) print(type(value)) print(list(value)) #將value組成的視圖轉(zhuǎn)成list #獲取所有的key-value值 items = scores.items() print(items) print(type(items)) print(list(items)) #轉(zhuǎn)換為list后元素由元組組成

四、字典的遍歷

for item in scores :
print(item,end=' ') #輸出的是字典中的key
#輸出key對(duì)應(yīng)的value
print(scores[item],end=' ')
print(scores.get(item))
五、字典的特點(diǎn)

六、字典生成式


students = ['mark','sheep','jerry','tom']
grades = [100,78,60,59]
d={key:price for key,price in zip(students,grades)}
print(d)
總結(jié)
本篇文章就到這里了,希望能夠給你帶來(lái)幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
引言

列表、字典:可變序列,可以執(zhí)行增刪改排序等
字典:無(wú)序的


一、字典的創(chuàng)建
#使用{}創(chuàng)建
scores = {'張三':100 ,'李四':98 ,'王麻子':72}
print(scores)
print(type(scores))
#使用內(nèi)置函數(shù)dict()
student = dict(name = 'jack ', age = 16)
print(student)
print(type(student))
二、字典元素的操作
(一)獲取

#獲取字典中的元素
#方法一:
print(scores['張三'])
#方法二:
print(scores.get('張三'))
print(scores.get('66'))
#如果查找的不存在,返回none
(二)增刪改

刪除操作
del scores['張三'] #根據(jù)索引刪除 key 和value print(scores) scores.clear() #刪除所有 print(scores)
新增操作 (直接增加)
scores['趙四'] = 80
三、獲取字典的視圖

# 獲取所有key值 key = scores.keys() print(key) print(type(key)) print(list(key)) #將key組成的視圖轉(zhuǎn)成list #獲取所有value值 value = scores.values() print(value) print(type(value)) print(list(value)) #將value組成的視圖轉(zhuǎn)成list #獲取所有的key-value值 items = scores.items() print(items) print(type(items)) print(list(items)) #轉(zhuǎn)換為list后元素由元組組成

四、字典的遍歷

for item in scores :
print(item,end=' ') #輸出的是字典中的key
#輸出key對(duì)應(yīng)的value
print(scores[item],end=' ')
print(scores.get(item))
五、字典的特點(diǎn)

六、字典生成式


students = ['mark','sheep','jerry','tom']
grades = [100,78,60,59]
d={key:price for key,price in zip(students,grades)}
print(d)
總結(jié)
本篇文章就到這里了,希望能夠給你帶來(lái)幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
Python3.6+selenium2.53.6自動(dòng)化測(cè)試_讀取excel文件的方法
這篇文章主要介紹了Python3.6+selenium2.53.6自動(dòng)化測(cè)試_讀取excel文件的方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-09-09
golang/python實(shí)現(xiàn)歸并排序?qū)嵗a
這篇文章主要給大家介紹了關(guān)于golang/python實(shí)現(xiàn)歸并排序的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
Python實(shí)現(xiàn)打印詳細(xì)報(bào)錯(cuò)日志,獲取報(bào)錯(cuò)信息位置行數(shù)
這篇文章主要介紹了Python實(shí)現(xiàn)打印詳細(xì)報(bào)錯(cuò)日志,獲取報(bào)錯(cuò)信息位置行數(shù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08
windows 10 設(shè)定計(jì)劃任務(wù)自動(dòng)執(zhí)行 python 腳本的方法
這篇文章主要介紹了windows 10 設(shè)定計(jì)劃任務(wù)自動(dòng)執(zhí)行 python 腳本的方法,本文圖文并茂給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-09-09
Python實(shí)現(xiàn)RGB與HSI顏色空間的互換方式
今天小編就為大家分享一篇Python實(shí)現(xiàn)RGB與HSI顏色空間的互換方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11

