遍历python字典几种方法总结(推荐)
如下所示:
aDict = {'key1':'value1', 'key2':'value2', 'key3':'value3'} print '-----------dict-------------' for d in aDict: print "%s:%s" %(d, aDict[d]) print '-----------item-------------' for (k,v) in aDict.items(): print '%s:%s' %(k, v) #效率最高 print '------------iteritems---------' for k,v in aDict.iteritems(): print '%s:%s' % (k, v) #最笨的方法 print '---------iterkeys---------------' for k in aDict.iterkeys(): print '%s:%s' % (k, aDict[k]) print '------------iterkeys, itervalues----------' for k,v in zip(aDict.iterkeys(), aDict.itervalues()): print '%s:%s' % (k, v)
运行结果:
<pre name="code" class="python">-----------dict------------- key3:value3 key2:value2 key1:value1 -----------item------------- key3:value3 key2:value2 key1:value1 ------------iteritems--------- key3:value3 key2:value2 key1:value1 ---------iterkeys--------------- key3:value3 key2:value2 key1:value1 ------------iterkeys, itervalues---------- key3:value3 key2:value2 key1:value1
相关推荐
YENCSDN 2020-11-17
lsjweiyi 2020-11-17
houmenghu 2020-11-17
Erick 2020-11-17
HeyShHeyou 2020-11-17
以梦为马不负韶华 2020-10-20
lhtzbj 2020-11-17
夜斗不是神 2020-11-17
pythonjw 2020-11-17
dingwun 2020-11-16
lhxxhl 2020-11-16
坚持是一种品质 2020-11-16
染血白衣 2020-11-16
huavhuahua 2020-11-20
meylovezn 2020-11-20
逍遥友 2020-11-20
weiiron 2020-11-16