Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 此代码中列表[:]的含义是什么?_Python_List_For Loop_Iteration - Fatal编程技术网

Python 此代码中列表[:]的含义是什么?

Python 此代码中列表[:]的含义是什么?,python,list,for-loop,iteration,Python,List,For Loop,Iteration,这段代码来自Python的文档。我有点困惑 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

这段代码来自Python的文档。我有点困惑

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)

为什么这段代码创建了一个无限循环而第一个没有呢?

这是一个陷阱!对于python来说,这可以逃避初学者

words[:]
是这里的神奇酱汁

注意:

>>> 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[:]
返回现有列表的
副本
,因此您正在迭代一个未修改的副本

您可以使用
id()
检查是否引用了相同的列表:

在第一种情况下:

>>> words2 = words[:]
>>> id(words2)
4360026736
>>> id(words)
4360188992
>>> words2 is words
False
for w in words[:]
在第二种情况下:

>>> id(words2)
4360188992
>>> id(words)
4360188992
>>> words2 is words
True
words = ['cat', 'window', 'defenestrate']
for w in words:
    if len(w) > 6:
        words.insert(0, w)
print(words)
值得注意的是,
[i:j]
被称为切片操作符,它所做的是返回列表的一个新副本,从索引
i
,一直到(但不包括)索引
j

所以,
单词[0:2]
给你

>>> words[0:2]
['hello', 'cat']
省略起始索引意味着它默认为
0
,而省略最后一个索引意味着它默认为
len(words)
,最终结果是您收到整个列表的副本


如果您想让代码更具可读性,我建议使用
copy
模块

from copy import copy 

words = ['cat', 'window', 'defenestrate']
for w in copy(words):
    if len(w) > 6:
        words.insert(0, w)
print(words)
这与您的第一个代码片段基本相同,并且可读性更高


或者(正如DSM在评论中提到的)在python>=3上,您也可以使用
words.copy()
,它做同样的事情

这是一个陷阱!对于python来说,这可以逃避初学者

words[:]
是这里的神奇酱汁

注意:

>>> 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[:]
返回现有列表的
副本
,因此您正在迭代一个未修改的副本

您可以使用
id()
检查是否引用了相同的列表:

在第一种情况下:

>>> words2 = words[:]
>>> id(words2)
4360026736
>>> id(words)
4360188992
>>> words2 is words
False
for w in words[:]
在第二种情况下:

>>> id(words2)
4360188992
>>> id(words)
4360188992
>>> words2 is words
True
words = ['cat', 'window', 'defenestrate']
for w in words:
    if len(w) > 6:
        words.insert(0, w)
print(words)
值得注意的是,
[i:j]
被称为切片操作符,它所做的是返回列表的一个新副本,从索引
i
,一直到(但不包括)索引
j

所以,
单词[0:2]
给你

>>> words[0:2]
['hello', 'cat']
省略起始索引意味着它默认为
0
,而省略最后一个索引意味着它默认为
len(words)
,最终结果是您收到整个列表的副本


如果您想让代码更具可读性,我建议使用
copy
模块

from copy import copy 

words = ['cat', 'window', 'defenestrate']
for w in copy(words):
    if len(w) > 6:
        words.insert(0, w)
print(words)
这与您的第一个代码片段基本相同,并且可读性更高

或者(正如DSM在评论中提到的)在python>=3上,您也可以使用
words.copy()
,它做同样的事情

(除了@Coldspeed answer)

请看以下示例:

words = ['cat', 'window', 'defenestrate']
words2 = words
words2 is words
结果:
True

它意味着名称
word
words2
指的是同一个对象

words = ['cat', 'window', 'defenestrate']
words2 = words[:]
words2 is words
结果:
False

在本例中,我们创建了新对象。

(除了@Coldspeed answer)

请看以下示例:

words = ['cat', 'window', 'defenestrate']
words2 = words
words2 is words
结果:
True

它意味着名称
word
words2
指的是同一个对象

words = ['cat', 'window', 'defenestrate']
words2 = words[:]
words2 is words
结果:
False


在本例中,我们创建了新对象。

words[:]
words
中的所有元素复制到一个新列表中。因此,当您迭代
words[:]
时,实际上是在迭代
words
当前拥有的所有元素。因此,当您修改
单词时,这些修改的效果在
单词[:]
中不可见(因为您在开始修改
单词之前调用了
单词[:]

在后一个示例中,您正在迭代
单词
,这意味着您对
单词
所做的任何更改都确实对迭代器可见。因此,当您在
单词
的索引0中插入时,您会将
单词
中的每个其他元素“增加”一个索引。因此,当您继续进行for循环的下一次迭代时,您将在
words
的下一个索引处获得元素,但这正是您刚才看到的元素(因为您在列表的开头插入了一个元素,将所有其他元素上移了一个索引)

要查看此操作,请尝试以下代码:

words = ['cat', 'window', 'defenestrate']
for w in words:
    print("The list is:", words)
    print("I am looking at this word:", w)
    if len(w) > 6:
        print("inserting", w)
        words.insert(0, w)
        print("the list now looks like this:", words)
print(words)

words[:]
words
中的所有元素复制到一个新列表中。因此,当您迭代
words[:]
时,实际上是在迭代
words
当前拥有的所有元素。因此,当您修改
单词时,这些修改的效果在
单词[:]
中不可见(因为您在开始修改
单词之前调用了
单词[:]

在后一个示例中,您正在迭代
单词
,这意味着您对
单词
所做的任何更改都确实对迭代器可见。因此,当您在
单词
的索引0中插入时,您会将
单词
中的每个其他元素“增加”一个索引。因此,当您继续进行for循环的下一次迭代时,您将在
words
的下一个索引处获得元素,但这正是您刚才看到的元素(因为您在列表的开头插入了一个元素,将所有其他元素上移了一个索引)

要查看此操作,请尝试以下代码:

words = ['cat', 'window', 'defenestrate']
for w in words:
    print("The list is:", words)
    print("I am looking at this word:", w)
    if len(w) > 6:
        print("inserting", w)
        words.insert(0, w)
        print("the list now looks like this:", words)
print(words)

让我们看看迭代器和iterables:

iterable是具有返回 迭代器,或它定义了一个可以 从零开始的顺序索引(当 索引不再有效)。所以一个可移植的