“ 記錄遍歷字典的幾種方式”
1 dict1={'name':'吳亦凡','age':'29','native':'廣州','opus':'大碗寬面'}
1 #遍歷字典key值---方法1 2 for key in dict1: 3 print(key) 4 5 # 遍歷字典key值---方法2 6 for key in dict1.keys(): 7 print(key) 8 9 #遍歷字典value值 10 for value in dict1.values(): 11 print(value) 12 13 #遍歷字典中的元素 14 for item in dict1.items(): 15 print(item)
輸出結果:
1 #遍歷字典key值---方法1 2 name 3 age 4 native 5 opus 6 7 #遍歷字典key值---方法2 8 name 9 age 10 native 11 opus 12 13 #遍歷字典value值 14 吳亦凡 15 29 16 廣州 17 大碗寬面 18 19 #遍歷字典中的元素 20 ('name', '吳亦凡') 21 ('age', '29') 22 ('native', '廣州') 23 ('opus', '大碗寬面')
