首页 > 编程语言 > 详细

python 循环技巧

时间:2015-04-26 15:20:56      阅读:254      评论:0      收藏:0      [点我收藏+]

摘自:超级无敌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.


python 循环技巧

原文:http://my.oschina.net/lcxidian/blog/406463

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!