(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