python字典遍历

1. 遍历key

dic = {'a': '1',   'b': '2',   'c': '3'}

for key in dic:
  print(dic[key])

 

#另一种方式, 更直白 

for key in dic.keys():
  print(key)

 

2. 遍历value

dic = {'a': '1',   'b': '2',   'c': '3'}

for value in dic.values():
  print(value)

 

3.遍历字典项

dic = {'a': '1',   'b': '2',   'c': '3'}

for item in dic.items():
  print(item)

 

#返回元组

('a', '1')
('b', '2')
('c', '3')


4. 同时遍历key和value

for key,value in dic.items():
  print(key + ':' + value)


 

 

 
posted @ 2018-12-24 16:24  coffee~  阅读(273)  评论(0编辑  收藏  举报