您的位置:首页 > 编程语言 > Python开发

python 循环技巧

2015-04-26 13:35 309 查看
摘自:超级无敌python教程

dict的循环:

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

list的循环:

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

多个list的循环:

>>> questions = ['name', 'quest', 'favorite color']
>>> answers = ['lancelot', '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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: