在Python中循环列表并修改它 [英] Loop through a list in Python and modify it
问题描述
words = ['cat','window','defenestrate']
对于w而言[:]:
if len(w)> 6:
words.insert(0,w)
print(words)
以下是我首先想到的:
words = ['cat','window','defenestrate']
for w in words:
if len(w)> 6:
words.insert(0,w)
print(words)
为什么这段代码创建了一个无限循环,而第一个没有?
这是一个难题!的python,可以逃脱初学者。
字[:]
是魔术酱。 观察:
>>> words = ['cat','window','defenestrate']
>>> words2 = words [:]
>>> words2.insert(0,'hello')
>>> words2
['hello','cat','window','defenestrate']
>>>单词
['cat','window','defenestrate']
[:]
:
>>> words = ['cat','window','defenestrate']
>>> words2 = words
>>> words2.insert(0,'hello')
>>> words2
['hello','cat','window','defenestrate']
>>>单词
['hello','cat','window','defenestrate']
这里需要注意的是 words [:]
返回现有列表的 copy
,所以你遍历未修改的副本。
您可以使用 id()
来检查您是否引用相同的列表:
在第一种情况下:
>>> words2 = words [:]
>>> id(words2)
4360026736
>>> id(words)
4360188992
>>> words2是单词
False
在第二种情况下:
>>> id(words2)
4360188992
>>> id(words)
4360188992
>>> words2 is words
True
值得注意的是 [i :j]
被称为切片操作符,它所做的是返回从索引 i
给你
>>> [0:2]
['hello','cat']
省略起始索引意味着它默认为 如果你想让你的代码更具可读性,我建议使用 这和你的第一个代码片段基本上是一样的,并且更具可读性。 或者(如注释中的DSM所述)和python> = 3 ,你也可以使用 This code is from Python's Documentation. I'm a little confused. And the following is what I thought at first: Why does this code create a infinite loop and the first one doesn't? This is one of the gotchas! of python, that can escape beginners. The Observe: And now without the The main thing to note here is that You can check whether you are referring to the same lists using In the first case: In the second case: It is worth noting that So, Omitting the starting index means it defaults to If you want to make your code a little more readable, I recommend the This basically does the same thing as your first code snippet, and is much more readable. Alternatively (as mentioned by DSM in the comments) and on python >=3, you may also use 这篇关于在Python中循环列表并修改它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! 0
,而省略最后一个索引意味着它默认为 len(words)
,最终的结果就是你收到了整个 列表的副本。
copy
模块。
从副本进口副本
words = ['cat','window','defenestrate']
for copy in copy(words):
if len(w)> ; 6:
words.insert(0,w)
print(words)
words.copy()
来做同样的事情。 words = ['cat', 'window', 'defenestrate']
for w in words[:]:
if len(w) > 6:
words.insert(0, w)
print(words)
words = ['cat', 'window', 'defenestrate']
for w in words:
if len(w) > 6:
words.insert(0, w)
print(words)
words[:]
is the magic sauce here. >>> words = ['cat', 'window', 'defenestrate']
>>> words2 = words[:]
>>> words2.insert(0, 'hello')
>>> words2
['hello', 'cat', 'window', 'defenestrate']
>>> words
['cat', 'window', 'defenestrate']
[:]
:>>> words = ['cat', 'window', 'defenestrate']
>>> words2 = words
>>> words2.insert(0, 'hello')
>>> words2
['hello', 'cat', 'window', 'defenestrate']
>>> words
['hello', 'cat', 'window', 'defenestrate']
words[:]
returns a copy
of the existing list, so you are iterating over a copy, which is not modified. id()
:>>> words2 = words[:]
>>> id(words2)
4360026736
>>> id(words)
4360188992
>>> words2 is words
False
>>> id(words2)
4360188992
>>> id(words)
4360188992
>>> words2 is words
True
[i:j]
is called the slicing operator, and what it does is it returns a fresh copy of the list starting from index i
, upto (but not including) index j
.words[0:2]
gives you>>> words[0:2]
['hello', 'cat']
0
, while omitting the last index means it defaults to len(words)
, and the end result is that you receive a copy of the entire list.
copy
module.from copy import copy
words = ['cat', 'window', 'defenestrate']
for w in copy(words):
if len(w) > 6:
words.insert(0, w)
print(words)
words.copy()
which does the same thing.