大家都列表遍历都比较熟悉,但是突然被问到字典遍历问题,可能被问蒙:
下边了解一下:
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)遍历value值
>>> for value in a.values(): print(value)
(3)遍历字典项
>>> for kv in a.items(): print(kv)
(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