python如何遍历dict
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)
来源:PY学习网:原文地址:https://www.py.cn/article.html