原文链接: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:1 b:2 c:3 >>> for key in a.keys(): print(key+‘:‘+a[key]) a:1 b:2 c:3
在使用上,for key in a和 for key in a.keys():完全等价。
(2)遍历value值
>>> for value in a.values(): print(value) 1 2 3
(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:1 b:2 c:3 >>> for (key,value) in a.items(): print(key+‘:‘+value) a:1 b:2 c:3
原文地址:https://www.cnblogs.com/m-zhang-yang/p/9123478.html
时间: 2024-10-10 16:46:45