python中如何遍历字典 您所在的位置:网站首页 字典的遍历python python中如何遍历字典

python中如何遍历字典

2022-05-22 05:02| 来源: 网络整理| 查看: 265

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中item()方法遍历字典? python中如何判断值在不在字典中 python中如何遍历字典 python dict怎么定义 教你用两种方式遍历循环python中的字典 强烈推荐Python新手学习之——字典 小白必看!深入探究Python中的字典容器 python怎么检查元素是否在列表中存在 python中怎么遍历字典 python字典如何相加



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

      专题文章
        CopyRight 2018-2019 实验室设备网 版权所有