python循环技巧
2016-08-08 19:16
232 查看
python循环技巧
在序列中循环时,索引位置和对应值可以使用 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 = ['lancelot', 'the holy grail', 'blue'] >>> for q, a in zip(questions, answers): ... print 'What is your {0}? It is {1}.'.format(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.
需要逆向循环序列的话,先正向定位序列,然后调用 reversed() 函数:
>>> for i in reversed(xrange(1, 10, 2)): ... print(i) ... 9 7 5 3 1
要按排序后的顺序循环序列的话,使用 sorted() 函数,它不改动原序列,而是生成一个新的已排序的序列:
>>> basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana'] >>> for f in sorted(set(basket)): ... print f ... apple banana orange pear
遍历字典时,使用
iteritems()方法可以同时得到键和对应的值。:
>>> knights = {'gallahad': 'the pure', 'robin': 'the brave'} >>> for k, v in knights.iteritems(): ... print k, v ... gallahad the pure robin the brave
若要在循环内部修改正在遍历的序列(例如复制某些元素),建议您首先制作副本。在序列上循环不会隐式地创建副本。切片表示法使这尤其方便:
>>> words = ['cat', 'window', 'defenestrate'] >>> for w in words[:]: # Loop over a slice copy of the entire list. ... if len(w) > 6: ... words.insert(0, w) ... >>> words ['defenestrate', 'cat', 'window', 'defenestrate']
reference:
http://www.pythondoc.com/pythontutorial27/datastructures.html#tut-loopidioms
相关文章推荐
- Python实用技巧---列表推导式(轻量级循环)与map
- Python列表生成器的循环技巧分享
- python循环技巧
- Python数据结构的一些技巧、数据结构练习、循环与判断练习题
- Python笔记7:循环技巧和控制语句
- Python列表生成器的循环技巧分享
- Python的for和break循环结构中使用else语句的技巧
- Python减少循环层次和缩进的技巧分析
- python函数定义中的参数问题、循环技巧
- python 循环技巧
- Python减少循环层次和缩进的技巧分析
- Python学习笔记:循环技巧
- Python -- 循环技巧(Looping Techniques)
- python循环技巧
- Python循环的技巧
- Python学习笔记-数据结构之循环技巧
- python 循环技巧
- 【python技巧系列】在循环中处理异常并继续运行
- Python的for和break循环结构中使用else语句的技巧
- Python技巧-警惕关键字