Python字典的遍历

x33g5p2x  于10个月前 转载在 Python  
字(0.8k)|赞(0)|评价(0)|浏览(101)

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}')

相关文章