在python中對字典進行遍歷的方法有以下幾種
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
2.通過字典項遍歷
>>> for kv in a.items():
print(kv)
('a', '1')
('b', '2')
('c', '3')
3.通過字典健值遍歷
>>> 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
4.通過value值遍歷
>>> for value in a.values():
print(value)
1
2
3