Python字典的遍历
Posted yangbocsu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python字典的遍历相关的知识,希望对你有一定的参考价值。
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字典的遍历的主要内容,如果未能解决你的问题,请参考以下文章