python如何遍历dict
2024-08-01
来源:骅佗教育
python遍历字典有以下几种方式
我们先创建一个字典 D={‘a’:1,‘b’:2,’b‘:3,’d‘:4}
1. 遍历key值
for key in a: pritn(key+':'+a[key]) for key in a.keys(): print(key+':'+a[key])
2.遍历value值
for value in a.values(): print(value)
3.遍历字典项
for kv in a.items(): print(kv) (‘a’,'1')(...)
4.遍历字典键值
for key,value in a.items(): print(key+':'+value) for (key,value) in a.items(): print(key+':'+value)