首頁技術文章正文

遍歷Python字典的幾種方法

更新時間:2021-12-21 來源:黑馬程序員 瀏覽量:

python字典

Python字典的for循環(huán)遍歷

1.  遍歷key的值

scores_dict = {'語文': 105, '數(shù)學': 140, '英語': 120}
for key in scores_dict:
print(key)
python字典遍歷Key值

2.   遍歷value的值

scores_dict = {'語文': 105, '數(shù)學': 140, '英語': 120}
for value in scores_dict.values():
    print(value)
python字典遍歷vlaue值

3.  遍歷字典鍵值對

scores_dict = {'語文': 105, '數(shù)學': 140, '英語': 120}
for key in scores_dict:
print(key + ":" + str(scores_dict[key]))    # 返回字符串
遍歷字典
scores_dict = {'語文': 105, '數(shù)學': 140, '英語': 120}
for i in scores_dict.items():
print(i)    # 返回元組

遍歷字典返回元組


scores_dict = {'語文': 105, '數(shù)學': 140, '英語': 120}
for key, value in scores_dict.items():
    print(key + ':' + str(value))
遍歷字典鍵值對

2. Python字典視頻教程

加QQ:435946716獲取上面視頻的全套資料【視頻+筆記+源碼】



猜你喜歡:

怎樣修改和增加字典中的元素?

Python字典有哪些常見操作?

python中的字典如何使用?

Python下載和安裝圖文教程[超詳細]【附贈19天全套Python視頻教程】

黑馬程序員Python+大數(shù)據開發(fā)課程

分享到:
在線咨詢 我要報名
和我們在線交談!