原文链接:https://blog.csdn.net/normang/article/details/55804231
(1)遍历key值
>>> a{'a': '1', 'b': '2', 'c': '3'}>>> for key in a: print(key+':'+a[key]) a:1b:2c:3>>> for key in a.keys(): print(key+':'+a[key]) a:1b:2c:3
在使用上,for key in a和 for key in a.keys():完全等价。
(2)遍历value值
>>> for value in a.values(): print(value) 123
(3)遍历字典项
>>> for kv in a.items(): print(kv) ('a', '1')('b', '2')('c', '3')
(4)遍历字典健值
>>> for key,value in a.items(): print(key+':'+value) a:1b:2c:3>>> for (key,value) in a.items(): print(key+':'+value) a:1b:2c:3