why not use original list when will modify it in for statement

简介:
因为如果words被修改的话, for可能无法终止. 例如
>>> words = ['cat', 'window', 'defenestrate']
>>> for w in words:
...   if len(w) > 6:
...     words.insert(0, w)
... 
^CTraceback (most recent call last):
  File "<stdin>", line 3, in <module>
KeyboardInterrupt
>>> len(words)
33697

所以建议使用words的拷贝, 例如words[:]就是一种简单的拷贝方法, 不需重新定义一个变量.
>>> words = ['cat', 'window', 'defenestrate']
>>> words[:]
['cat', 'window', 'defenestrate']
>>> for w in words[:]:
...   if len(w) > 6:
...     words.insert(0, w)
... 
>>> words
['defenestrate', 'cat', 'window', 'defenestrate']


[参考]

4.2. for Statements

The for statement in Python differs a bit from what you may be used to in C or Pascal. Rather than always iterating over an arithmetic progression of numbers (like in Pascal), or giving the user the ability to define both the iteration step and halting condition (as C), Python’s for statement iterates over the items of any sequence (a list or a string), in the order that they appear in the sequence. For example (no pun intended):

>>>
>>> # Measure some strings:
... words = ['cat', 'window', 'defenestrate']
>>> for w in words:
...     print(w, len(w))
...
cat 3
window 6
defenestrate 12

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

>>>
>>> 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']
相关文章
java.lang.Error: Unresolved compilation problem: The type List is not generic; it cannot be parame
java.lang.Error: Unresolved compilation problem: The type List is not generic; it cannot be parame
9:32 Emulator: emulator: ERROR: Unknown AVD name [Nexus_5X_API_28], use -list-avds to see valid list
9:32 Emulator: emulator: ERROR: Unknown AVD name [Nexus_5X_API_28], use -list-avds to see valid list. 9:32 Emulator: Process finished with exit code 1
117 1
9:32 Emulator: emulator: ERROR: Unknown AVD name [Nexus_5X_API_28], use -list-avds to see valid list
使用代码获得table whered use list
使用代码获得table whered use list
Column \&#39;表名.某列名\&#39; is invalid in the select list because it is not contained in either an aggregate f
汉语:这个错误是提示'表名.某列名' 字段不在GROUP BY中 解决方案:group by 后面是要跟着的 select 中所有不是聚合函数的字段
1087 0
|
1月前
|
存储 安全 Java
java集合框架及其特点(List、Set、Queue、Map)
java集合框架及其特点(List、Set、Queue、Map)
|
23天前
|
Java
Java使用List去重的四中方式
Java使用List去重的四中方式
17 6
|
1月前
|
Java
JAVA——List中剔除空元素(null)的三种方法汇总
JAVA——List中剔除空元素(null)的三种方法汇总
|
1月前
|
安全 Java API
Java并发 - J.U.C并发容器类 list、set、queue
Queue API 阻塞是通过 condition 来实现的,可参考 Java 并发 - Lock 接口 ArrayBlockingQueue 阻塞 LinkedBlockingQueue 阻塞 ArrayQueue 非阻塞 LinkedQueue 非阻塞
|
1月前
|
存储 安全 Java
【Java】集合(一)单列集合List
【Java】集合(一)单列集合List
22 0
|
1月前
|
Java API
java 对象list 使用stream进行过滤
在Java中,你可以使用Stream API对对象列表进行过滤。假设你有一个`List<MyObject>`,并且你想根据某些条件过滤出特定的对象。以下是一个示例: ```java import java.util.List; import java.util.stream.Collectors; public class Main { public static void main(String[] args) { List<MyObject> myObjects = ... // 初始化你的对象列表 List<MyObject> filter