Python字典的遍历

Posted yangbocsu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python字典的遍历相关的知识,希望对你有一定的参考价值。


1 遍历字典的key

#1 遍历字典的key
info = {"name":"Tom","sex":"male","age":27}
for k in info.keys():
    print(k)

2 遍历字典的value

#2 遍历字典的value
info = {"name":"Tom","sex":"male","age":27}
for k in info.keys():
    print(info[k])
#2 遍历字典的value
info = {"name":"Tom","sex":"male","age":27}
for v in info.values():
    print(v) 

3 遍历字典的元素

#3 遍历字典的元素   type(items)  是元组形式的  <class 'tuple'>
info = {"name":"Tom","sex":"male","age":27}
for items in info.items():
    print(items)

4 遍历字典的键值对

#4 遍历字典的键值对
info = {"name":"Tom","sex":"male","age":27}
for key,value in info.items() :
    print(f'{key} = {value}')

以上是关于Python字典的遍历的主要内容,如果未能解决你的问题,请参考以下文章

Python snippet(代码片段)

Python代码阅读(第26篇):将列表映射成字典

Python代码阅读(第40篇):通过两个列表生成字典

python循环遍历字典元素问题求指教

python字典的遍历

Python 3.5 遍历字典列表