我在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 [:]