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
|
8月前
|
SQL 分布式计算 DataWorks
DataWorks操作报错合集之在使用函数holiday_date遇到报错: “Perhaps you forgot to add it to using list when create funciton.,是什么原因
DataWorks是阿里云提供的一站式大数据开发与治理平台,支持数据集成、数据开发、数据服务、数据质量管理、数据安全管理等全流程数据处理。在使用DataWorks过程中,可能会遇到各种操作报错。以下是一些常见的报错情况及其可能的原因和解决方法。
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
176 1
9:32 Emulator: emulator: ERROR: Unknown AVD name [Nexus_5X_API_28], use -list-avds to see valid 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 中所有不是聚合函数的字段
1118 0
|
9月前
|
安全 Java
java线程之List集合并发安全问题及解决方案
java线程之List集合并发安全问题及解决方案
1135 1
|
8月前
|
Java API Apache
怎么在在 Java 中对List进行分区
本文介绍了如何将列表拆分为给定大小的子列表。尽管标准Java集合API未直接支持此功能,但Guava和Apache Commons Collections提供了相关API。
|
8月前
|
运维 关系型数据库 Java
PolarDB产品使用问题之使用List或Range分区表时,Java代码是否需要进行改动
PolarDB产品使用合集涵盖了从创建与管理、数据管理、性能优化与诊断、安全与合规到生态与集成、运维与支持等全方位的功能和服务,旨在帮助企业轻松构建高可用、高性能且易于管理的数据库环境,满足不同业务场景的需求。用户可以通过阿里云控制台、API、SDK等方式便捷地使用这些功能,实现数据库的高效运维与持续优化。
|
8月前
|
存储 安全 Java
详解Java中集合的List接口实现的ArrayList方法 | Set接口实现的HashSet方法
详解Java中集合的List接口实现的ArrayList方法 | Set接口实现的HashSet方法
|
9月前
|
Java API
使用 Java 来实现两个 List 的差集操作
使用 Java 来实现两个 List 的差集操作
299 3