小编May*_*wal的帖子

我碰巧发现了这段代码:“用for for in word :,该示例将尝试创建一个无限列表

我在python doc文章4.2中找不到有关for循环的代码说明。

它提到了类似的内容:如果我们不复制列表,那么它将为列表打印无限的列表值words = ['cat', 'window', 'defenestrate'];但如果我们事先使用复制它,则不会"for w in words[:]"。我需要对此进行解释。

words = ['cat', 'window', 'defenestrate']
for w in words :
    if len(w) > 6:
       words.insert(0,w)
Run Code Online (Sandbox Code Playgroud)

该代码将在无限循环中运行,但是如果我们将for w inword with中的w 交换为word [:]

python list infinite python-3.x

6
推荐指数
1
解决办法
125
查看次数

标签 统计

infinite ×1

list ×1

python ×1

python-3.x ×1