- 遍历key值:
# 方式一:
dic = {1:"a",2:"b",3:"c"}
for k in dic:
print(k)
方式二:
for k in dic.keys():
print(k)
---------------------------
1
2
3
- 遍历value值
for v in dic.values():
print(v)
------------------------
a
b
c
- 遍历字典项,返回值为元组型(tuple)
for item in dic.items():
print(item)
------------------------------
(1, 'a')
(2, 'b')
(3, 'c')
<class 'tuple'>
- 遍历字典键值
for key,value in dic.items():
print(key,':',value)
------------------------------
1 : a
2 : b
3 : c