码迷,mamicode.com
首页 > 编程语言 > 详细

python:循环问题?

时间:2015-06-22 20:29:34      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:

To change a sequence you are iterating over while inside the loop (for example to duplicate certain items), it is recommended that you first make a copy. Looping over a sequence does not implicitly make a copy. The slice notation makes this especially convenient:

>>> 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‘]

在使用words循环时,会发生无限循环,这是个问题

python:循环问题?

标签:

原文地址:http://www.cnblogs.com/starnix/p/4593707.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!