不理解切片在 python 中的作用
Not understanding what this is doing in python with slicing
设置如下:
words = ['cat', 'window', 'defenestrate']
for w in words:
print(w, len(w))
然后我输入这个:
>>> for w in words[:]: # Loop over a slice copy of the entire list.
... if len(w) > 6:
... words.insert(0, w)
...
我不明白 insert(0, w)
中的 0 是什么
正在做。当我用另一个号码更改它时
输出保持不变。
决赛 Input/Output:
>>> words
['defenestrate', 'cat', 'window', 'defenestrate']
insert
的第一个参数告诉 insert
将新项目放在列表中的哪个索引处。由于您只有一个超过 6 个字符的单词,因此您发布的代码将 defenestrate
添加到现有列表的开头。
来自docs:
list.insert(i, x)
Insert an item at a given position. The first argument is the index of the element before which to insert, so a.insert(0, x) inserts at the front of the list, and a.insert(len(a), x) is equivalent to a.append(x).
改变插入位置应该改变输出,如下所示。插入位置(如您所说的“0”)告诉要插入列表中的哪个索引之前。
>>> words = ['cat', 'window', 'defenestrate']
>>> for w in words[:]:
... if len(w) > 6:
... words.insert(0,w)
...
>>> words
['defenestrate', 'cat', 'window', 'defenestrate']
>>> words = ['cat', 'window', 'defenestrate']
>>> for w in words[:]:
... if len(w) > 6:
... words.insert(2,w)
...
>>> words
['cat', 'window', 'defenestrate', 'defenestrate']
(0, w)
中的0
是要插入单词(w
)的索引。
所以您的代码基本上是遍历列表并将长度超过 6 个字符的任何单词放在列表的开头。
对我来说,更改索引的效果与您预期的一样。
索引 0
In [1]: words = ['cat', 'window', 'defenestrate']
In [2]: for w in words[:]:
...: if len(w) > 6:
...: words.insert(0, w)
...:
In [3]: words
Out[3]: ['defenestrate', 'cat', 'window', 'defenestrate']
索引 1
In [1]: words = ['cat', 'window', 'defenestrate']
In [2]: for w in words[:]:
if len(w) > 6:
words.insert(1, w)
...:
In [3]: words
Out[3]: ['cat', 'defenestrate', 'window', 'defenestrate']
索引 3
In [1]: words = ['cat', 'window', 'defenestrate']
In [2]: for w in words[:]:
if len(w) > 6:
words.insert(3, w)
...:
In [3]: words
Out[3]: ['cat', 'window', 'defenestrate', 'defenestrate']
I don't understand what the 0 in insert(0, w) is doing. When I change it with another number the output stays the same.
>>> for w in words[:]: # Loop over a slice copy of the entire list.
... if len(w) > 6:
... words.insert(0, w)
...
我以前在 python 教程中遇到过这个问题,这里是我的解释。
让我们回头看看上下文:
此示例来自 python 教程的第 4 章,但您提出问题的代码行引用了同一教程的第 5.1 章。
列表中的 'insert' 方法在这里:
https://docs.python.org/2.7/tutorial/datastructures.html
如果你正在做教程'in order',那么混淆是可以理解的,当然,你应该是,那么你不熟悉那行代码。跳到第 5.1 章,看看这个:
list.insert(i, x) Insert an item at a given position. The first
argument is the index of the element before which to insert, so
a.insert(0, x) inserts at the front of the list, and a.insert(len(a),
x) is equivalent to a.append(x).
作者试图表明的观点是,您希望在循环内修改列表之前制作列表的副本,否则 'bad things'™ 可能会发生。
参见:Thou Shalt Not Modify A List During Iteration
如果不复制列表,循环永远不会结束...试试这个:
>>> for w in words: # Loop over the list, and modify it while looping
... if len(w) > 6:
... words.insert(0, w)
...
您将一直等到 运行 内存不足,但您得到的是:
['defenestrate', 'defenestrate', 'defenestrate', 'defenestrate', 'defenestrate', 'defenestrate', 'defenestrate', 'defenestrate', 'defenestrate', 'defenestrate', 'cat', 'window', 'defenestrate']
原因是这样的:
- for 循环从
words[0]
获取 'w':'cat'
len('cat') > 6
是 false
- 继续
- for 循环从
words[1]
获取 'w':'window'
len('window') > 6
是 false
- 继续
- for 循环从
words[2]
获取 'w':'defenestrate'
len('defenstrate') > 6
是 true
- 插入...
words
现在是 ['defenestrate', 'cat', 'window', 'defenestrate']
- for 循环从
words[3]
获取 'w':'defenstrate'
len('defenstrate') > 6
是 true
- 插入...
words
现在是 ['defenstrate, 'defenestrate', 'cat', 'window', 'defenestrate']
- for 循环从
words[4]
获取 'w':'defenstrate'
... 继续无限。
复制列表时,会发生这种情况:
- for 循环从
words[3]
的副本中获取 'w'
- 引发异常:IndexError(因为
words
的副本只有 3 项长)
- for 循环通过终止循环来处理异常。
您从未更改 'words' 列表的副本。
您正在更改原始 'words' 列表。
副本是匿名的。它是一个仅用于控制循环的变量,并且仅在循环内部可用。它在循环终止时消失。
设置如下:
words = ['cat', 'window', 'defenestrate']
for w in words:
print(w, len(w))
然后我输入这个:
>>> for w in words[:]: # Loop over a slice copy of the entire list.
... if len(w) > 6:
... words.insert(0, w)
...
我不明白 insert(0, w)
中的 0 是什么
正在做。当我用另一个号码更改它时
输出保持不变。
决赛 Input/Output:
>>> words
['defenestrate', 'cat', 'window', 'defenestrate']
insert
的第一个参数告诉 insert
将新项目放在列表中的哪个索引处。由于您只有一个超过 6 个字符的单词,因此您发布的代码将 defenestrate
添加到现有列表的开头。
来自docs:
list.insert(i, x)
Insert an item at a given position. The first argument is the index of the element before which to insert, so a.insert(0, x) inserts at the front of the list, and a.insert(len(a), x) is equivalent to a.append(x).
改变插入位置应该改变输出,如下所示。插入位置(如您所说的“0”)告诉要插入列表中的哪个索引之前。
>>> words = ['cat', 'window', 'defenestrate']
>>> for w in words[:]:
... if len(w) > 6:
... words.insert(0,w)
...
>>> words
['defenestrate', 'cat', 'window', 'defenestrate']
>>> words = ['cat', 'window', 'defenestrate']
>>> for w in words[:]:
... if len(w) > 6:
... words.insert(2,w)
...
>>> words
['cat', 'window', 'defenestrate', 'defenestrate']
(0, w)
中的0
是要插入单词(w
)的索引。
所以您的代码基本上是遍历列表并将长度超过 6 个字符的任何单词放在列表的开头。
对我来说,更改索引的效果与您预期的一样。
索引 0
In [1]: words = ['cat', 'window', 'defenestrate']
In [2]: for w in words[:]:
...: if len(w) > 6:
...: words.insert(0, w)
...:
In [3]: words
Out[3]: ['defenestrate', 'cat', 'window', 'defenestrate']
索引 1
In [1]: words = ['cat', 'window', 'defenestrate']
In [2]: for w in words[:]:
if len(w) > 6:
words.insert(1, w)
...:
In [3]: words
Out[3]: ['cat', 'defenestrate', 'window', 'defenestrate']
索引 3
In [1]: words = ['cat', 'window', 'defenestrate']
In [2]: for w in words[:]:
if len(w) > 6:
words.insert(3, w)
...:
In [3]: words
Out[3]: ['cat', 'window', 'defenestrate', 'defenestrate']
I don't understand what the 0 in insert(0, w) is doing. When I change it with another number the output stays the same.
>>> for w in words[:]: # Loop over a slice copy of the entire list.
... if len(w) > 6:
... words.insert(0, w)
...
我以前在 python 教程中遇到过这个问题,这里是我的解释。
让我们回头看看上下文:
此示例来自 python 教程的第 4 章,但您提出问题的代码行引用了同一教程的第 5.1 章。
列表中的 'insert' 方法在这里: https://docs.python.org/2.7/tutorial/datastructures.html
如果你正在做教程'in order',那么混淆是可以理解的,当然,你应该是,那么你不熟悉那行代码。跳到第 5.1 章,看看这个:
list.insert(i, x) Insert an item at a given position. The first argument is the index of the element before which to insert, so a.insert(0, x) inserts at the front of the list, and a.insert(len(a), x) is equivalent to a.append(x).
作者试图表明的观点是,您希望在循环内修改列表之前制作列表的副本,否则 'bad things'™ 可能会发生。
参见:Thou Shalt Not Modify A List During Iteration
如果不复制列表,循环永远不会结束...试试这个:
>>> for w in words: # Loop over the list, and modify it while looping
... if len(w) > 6:
... words.insert(0, w)
...
您将一直等到 运行 内存不足,但您得到的是:
['defenestrate', 'defenestrate', 'defenestrate', 'defenestrate', 'defenestrate', 'defenestrate', 'defenestrate', 'defenestrate', 'defenestrate', 'defenestrate', 'cat', 'window', 'defenestrate']
原因是这样的:
- for 循环从
words[0]
获取 'w':'cat'len('cat') > 6
是false
- 继续
- for 循环从
words[1]
获取 'w':'window'len('window') > 6
是false
- 继续
- for 循环从
words[2]
获取 'w':'defenestrate'len('defenstrate') > 6
是true
- 插入...
words
现在是['defenestrate', 'cat', 'window', 'defenestrate']
- for 循环从
words[3]
获取 'w':'defenstrate'len('defenstrate') > 6
是true
- 插入...
words
现在是['defenstrate, 'defenestrate', 'cat', 'window', 'defenestrate']
- for 循环从
words[4]
获取 'w':'defenstrate'
... 继续无限。
复制列表时,会发生这种情况:
- for 循环从
words[3]
的副本中获取 'w'- 引发异常:IndexError(因为
words
的副本只有 3 项长) - for 循环通过终止循环来处理异常。
- 引发异常:IndexError(因为
您从未更改 'words' 列表的副本。
您正在更改原始 'words' 列表。
副本是匿名的。它是一个仅用于控制循环的变量,并且仅在循环内部可用。它在循环终止时消失。