导语:
本文主要介绍了关于python中如何遍历字典的相关知识,包括如何遍历字典,以及python 遍历列表这些编程知识,希望对大家有参考作用。
1. 遍历字典的键key
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)
②
>>> 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单引号错误怎么办?12/20
- ♥ openpyxl如何在sheet中读写数据09/18
- ♥ python如何加载Pillow包12/31
- ♥ 什么是python内置作用域12/09
- ♥ Python中的zfill()有什么用?10/01
- ♥ Python yield 关键字的应用限制01/13
内容反馈