python字典遍历的方法有哪些?
704次观看
标签:
遍历
字典
方法
老师回答
1、遍历key
>>> d
{'set': {'c', 'a', 'b'}, 'list': [1, 2, 3], 'tuple': (4, 5, 6), 'version': 'python3.7.4'}
>>> for k in d.keys():
... print(k)
...
set
list
tuple
version
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、遍历字典项
>>> for kv in a.items():
print(kv)
('a', '1')
('b', '2')
('c', '3')
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)
©本文版权归环球青藤所有,任何形式转载请联系我们。
免费直播
精选课程
相关推荐