python语言中如何遍历字典

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)

posted @ 2019-05-20 14:30  logo_mm  阅读(3846)  评论(0编辑  收藏  举报