python遍历字典的四种方法
- 根据key值遍历
>>> a {‘a‘: ‘1‘, ‘b‘: ‘2‘, ‘c‘: ‘3‘} >>> for key in a: print(key+‘:‘+a[key]) a:1 b:2 c:3 >>> for key in a.keys(): print(key+‘:‘+a[key]) a:1 b:2 c:3 根据value遍历 >>> for value in a.values(): print(value) 1 2 3 根据字典项遍历 >>> for kv in a.items(): print(kv) (‘a‘, ‘1‘) (‘b‘, ‘2‘) (‘c‘, ‘3‘) 根据字典键值对遍历 >>> for key,value in a.items(): print(key+‘:‘+value) a:1 b:2 c:3 >>> for (key,value) in a.items(): print(key+‘:‘+value) a:1 b:2 c:3
相关推荐
非攻 2020-10-25
youandme 2020-05-11
haokele 2020-04-23
paopao00 2020-04-07
zhangpan 2020-02-23
wyqwilliam 2020-02-14
Winterto0 2020-01-13
JackLang 2019-12-28
LczPtr 2019-12-24
georgeandgeorge 2019-12-09
wordmhg 2019-11-01
小炮集揣 2019-05-03
ciqingloveless 2019-06-30
yuan00yu 2019-06-28
Kwong 2019-06-27
远哥的小迷弟 2019-03-08
一叶不知秋 2018-01-23