在Python中循环列表并修改它 [英] Loop through a list in Python and modify it

查看:823
本文介绍了在Python中循环列表并修改它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这段代码来自Python的文档。

  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']

省略起始索引意味着它默认为 0 ,而省略最后一个索引意味着它默认为 len(words),最终的结果就是你收到了整个 列表的副本。




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

 从副本进口副本

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

这和你的第一个代码片段基本上是一样的,并且更具可读性。



或者(如注释中的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.

这篇关于在Python中循环列表并修改它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆