本文介绍了这段代码中 list[:] 是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码来自 Python 的文档.我有点糊涂了.

words = ['cat', 'window', 'defenestrate']对于 w in words[:]:如果 len(w) >6:word.insert(0, w)打印(字)

以下是我最初的想法:

words = ['cat', 'window', 'defenestrate']对于 w 的话:如果 len(w) >6:word.insert(0, w)打印(字)

为什么这段代码会创建一个无限循环而第一个不会?

解决方案

这是问题之一!的python,可以让初学者逃脱.

words[:] 是这里的魔法酱.

观察:

>>>词 = ['猫', '窗口', 'defenestrate']>>>单词 2 = 单词 [:]>>>words2.insert(0, '你好')>>>词2['你好', '猫', '窗口', 'defenestrate']>>>字['猫', '窗口', 'defenestrate']

现在没有 [:]:

>>>词 = ['猫', '窗口', 'defenestrate']>>>单词 2 = 单词>>>words2.insert(0, '你好')>>>词2['你好', '猫', '窗口', 'defenestrate']>>>字['你好', '猫', '窗口', 'defenestrate']

此处要注意的主要事项是 words[:] 返回现有列表的 copy,因此您正在迭代未修改的副本.

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

在第一种情况下:

>>>单词 2 = 单词 [:]>>>id(words2)4360026736>>>身份证(字)4360188992>>>words2 是词错误的

第二种情况:

>>>id(words2)4360188992>>>身份证(字)4360188992>>>words2 是词真的

值得注意的是[i:j]被称为切片操作,它的作用是返回一个从索引i,最多(但不包括)索引j.

所以,words[0:2] 给你

>>>词[0:2]['你好','猫']

省略起始索引表示默认为0,省略最后一个索引表示默认为len(words),最终结果是你收到一个整个列表的副本.

如果你想让你的代码更具可读性,我推荐copy模块.

from copy import copy词 = ['猫', '窗口', 'defenestrate']对于副本中的 w(单词):如果 len(w) >6:word.insert(0, w)打印(字)

这基本上与您的第一个代码片段做同样的事情,并且更具可读性.

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

This code is from Python's Documentation. I'm a little confused.

words = ['cat', 'window', 'defenestrate']
for w in words[:]:
    if len(w) > 6:
        words.insert(0, w)
print(words)

And the following is what I thought at first:

words = ['cat', 'window', 'defenestrate']
for w in words:
    if len(w) > 6:
        words.insert(0, w)
print(words)

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 words[:] is the magic sauce here.

Observe:

>>> words =  ['cat', 'window', 'defenestrate']
>>> words2 = words[:]
>>> words2.insert(0, 'hello')
>>> words2
['hello', 'cat', 'window', 'defenestrate']
>>> words
['cat', 'window', 'defenestrate']

And now without the [:]:

>>> words =  ['cat', 'window', 'defenestrate']
>>> words2 = words
>>> words2.insert(0, 'hello')
>>> words2
['hello', 'cat', 'window', 'defenestrate']
>>> words
['hello', 'cat', 'window', 'defenestrate']

The main thing to note here is that words[:] returns a copy of the existing list, so you are iterating over a copy, which is not modified.

You can check whether you are referring to the same lists using id():

In the first case:

>>> words2 = words[:]
>>> id(words2)
4360026736
>>> id(words)
4360188992
>>> words2 is words
False

In the second case:

>>> id(words2)
4360188992
>>> id(words)
4360188992
>>> words2 is words
True

It is worth noting that [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.

So, words[0:2] gives you

>>> words[0:2]
['hello', 'cat']

Omitting the starting index means it defaults to 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.


If you want to make your code a little more readable, I recommend the 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)

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 words.copy() which does the same thing.

这篇关于这段代码中 list[:] 是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 13:54