Kwong 2019-12-26
“ 记录遍历字典的几种方式”
#遍历字典key值---方法1
for key in dict1:
print(key)
# 遍历字典key值---方法2
for key in dict1.keys():
print(key)
#遍历字典value值
for value in dict1.values():
print(value)
#遍历字典中的元素
for item in dict1.items():
print(item)输出结果:
#遍历字典key值---方法1 name age native opus #遍历字典key值---方法2 name age native opus #遍历字典value值 吴亦凡 29 广州 大碗宽面 #遍历字典中的元素 (‘name‘, ‘吴亦凡‘) (‘age‘, ‘29‘) (‘native‘, ‘广州‘) (‘opus‘, ‘大碗宽面‘)
python中,json和dict非常类似,都是key-value的形式,而且json、dict也可以非常方便的通过dumps、loads互转。既然都是key-value格式,为啥还需要进行格式转换?