Python字典遍历
字典遍历
<1> 遍历字典的key(键)
<2> 遍历字典的value(值)
<3> 遍历字典的项(元素)
<4> 遍历字典的key-value(键值对)
想一想,如何实现带下标索引的遍历
>>> chars = [‘a‘, ‘b‘, ‘c‘, ‘d‘] >>> i = 0 >>> for chr in chars: ... print("%d %s"%(i, chr)) ... i += 1 ... 0 a 1 b 2 c 3 d
enumerate()
>>> chars = [‘a‘, ‘b‘, ‘c‘, ‘d‘] >>> for i, chr in enumerate(chars): ... print i, chr ... 0 a 1 b 2 c 3 d
相关推荐
LHpython 2016-05-12