python 循环

字典 items

knights = {'gallahad':'the pure',
            'robin':'the brave'}
for k,v in knights.items():
    print k,v
>>> galahad the pure
>>> robin the brave

序列 enumerate

for i,v in enumerate(['tic','tac','toe']):
    print i,v
>>> 0 tic
>>> 1 tac
>>> 2 toe

多序列 zip

questions = ['name','quest','favorite color']
answers = ['adou','the holy grail','blue']
for q,a in zip(questions,answers):
    print 'What is your %s? It is %s.' % (q,a)

>>> What is your name?  It is lancelot.
>>> What is your quest?  It is the holy grail
>>> What is your favorite color?  It is blue.