The following is a summary of several methods to traverse the python dictionary (recommended ). I think this is quite good. now I will share it with you and give you a reference. Let's take a look at the following small series to bring you a summary of several methods to traverse the python dictionary (recommended ). I think this is quite good. now I will share it with you and give you a reference. Let's take a look at it with Xiaobian.
As follows:
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) # The most efficient print' ------------ iteritems --------- 'for k, v in aDict. iteritems (): print '% s: % s' % (k, v) # The most stupid method 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)
Running result:
-----------dict-------------key3:value3key2:value2key1:value1-----------item-------------key3:value3key2:value2key1:value1------------iteritems---------key3:value3key2:value2key1:value1---------iterkeys---------------key3:value3key2:value2key1:value1------------iterkeys, itervalues----------key3:value3key2:value2key1:value1
The summary (recommended) of the above several methods for traversing the python dictionary is all the content shared by the editor. I hope to give you a reference and support for PHP.
For more information about how to traverse the python dictionary, see the Chinese PHP website!