python中如何遍历字典
发布时间:2021-05-15 编辑:jiaochengji.com
教程集为您提供python中如何遍历字典等资源,欢迎您收藏本站,我们将为您提供最新的python中如何遍历字典资源
1. 遍历字典的键key
python学习网,大量的免费python视频教程,欢迎在线学习!
①
>>> d={'list':[1, 2, 3],1:123,'111':'python3','tuple':(4, 5, 6)} >>> for key in d: print(str(key) ':' str(d[key])) list:[1, 2, 3] 1:123 111:python3 tuple:(4, 5, 6)
相关推荐:《Python教程》
②
>>> d={'list':[1, 2, 3],1:123,'111':'python3','tuple':(4, 5, 6)} >>> for key in d.keys(): print(key) 1 list 111 tuple
2. 遍历字典的值value
>>> d={'list':[1, 2, 3],1:123,'111':'python3','tuple':(4, 5, 6)} >>> for value in d.values(): print (value) [1, 2, 3] 123 python3 (4, 5, 6)
3. 遍历字典的项
>>> d={'list':[1, 2, 3],1:123,'111':'python3','tuple':(4, 5, 6)} >>> for item in d.items(): print(item) ('list', [1, 2, 3]) (1, 123) ('111', 'python3') ('tuple', (4, 5, 6))
4. 遍历字典的key-value
①
>>> d={'list':[1, 2, 3],1:123,'111':'python3','tuple':(4, 5, 6)} >>> for key,value in d.items(): print(key,value) list [1, 2, 3] 1 123 111 python3 tuple (4, 5, 6)
②
>>> d={'list':[1, 2, 3],1:123,'111':'python3','tuple':(4, 5, 6)} >>> for (key,value) in d.items(): print(key,value) list [1, 2, 3] 1 123 111 python3 tuple (4, 5, 6)
您可能感兴趣的文章:
python中如何判断值在不在字典中
如何使用Python中item()方法遍历字典?
python中如何遍历字典
python dict怎么定义
教你用两种方式遍历循环python中的字典
小白必看!深入探究Python中的字典容器
python怎么检查元素是否在列表中存在
python字典如何相加
强烈推荐Python新手学习之——字典
python中怎么遍历字典
上一篇:python如何整体缩进
下一篇:vscode如何配置python
[关闭]