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)

猜你喜欢

转载自www.cnblogs.com/amy7758/p/10893984.html