如何在循环中列出列表项? [英] How to del item of a list in loop?

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

问题描述



大家好,这是我在这个新闻组中的第一篇文章。

我是python的新手虽然我有几年的c ++开发经验。

最近,当我在迭代时尝试del列表项时,我感到难过。


这是我做错的方式:


lst = [1,2,3]

for i in lst:

print i

if i == 2:

lst.remove(i)


结果是:


1

2


Hi everybody, it is my first post in this newsgroup.
I am a newbie for python though I have several years development experience in c++.
recently, I was stumped when I tried to del item of a list when iteration.

here is the wrong way I did:

lst = [1, 2, 3]
for i in lst:
print i
if i == 2:
lst.remove(i)

the result is:

1
2




如你所见,3缺失。这个问题是由''lst.remove(i)''引起的。

显然,''标记 - 扫描''是解决这个问题的解决方案。

但我认为应该更明智的伎俩。我想得到你的帮助。


提前致谢。


- 头骨



as you would see, ''3'' is missing. this problem is caused by ''lst.remove(i)''.
apparently, ''marked-and-sweep'' is a solution to deal with this issue.
but I think there SHOULD BE more ''wise'' trick. I want to get your help.

Thanks in advance.

- skull

推荐答案

skull写道:
大家好,这是我在这个新闻组中的第一篇文章。
我是python的新手虽然我有几年的c ++开发经验最近,当我在迭代时尝试del列表项时,我感到难过。

这是我做错的方式:

lst = [1 ,2,3]
我在lst:
打印我
如果我= = 2:
lst.remove(i)

结果是:

1
2
Hi everybody, it is my first post in this newsgroup.
I am a newbie for python though I have several years development experience in c++.
recently, I was stumped when I tried to del item of a list when iteration.

here is the wrong way I did:

lst = [1, 2, 3]
for i in lst:
print i
if i == 2:
lst.remove(i)

the result is:

1
2



会看到,''3''缺失。这个问题是由''lst.remove(i)''引起的。
显然,''标记和扫描''是解决这个问题的解决方案。
但我认为应该是更明智的伎俩。我想得到你的帮助。



as you would see, ''3'' is missing. this problem is caused by ''lst.remove(i)''.
apparently, ''marked-and-sweep'' is a solution to deal with this issue.
but I think there SHOULD BE more ''wise'' trick. I want to get your help.




快速解决方案:


for i in lst [:]


遍历副本。


Reinhold



Quick solution:

for i in lst[:]

iterates over a copy.

Reinhold


" skull"写道:
"skull" wrote:
大家好,这是我在这个新闻组中的第一篇文章。
我是python的新手,虽然我有几年的c ++开发经验。
最近,当我在迭代时尝试del列表项时,我感到很难过。

这是我做错的方式:

lst = [1,2,3]
我在lst:
打印我
如果我= = 2:
lst.remove(i)

结果是:

1
2
Hi everybody, it is my first post in this newsgroup.
I am a newbie for python though I have several years development experience in c++.
recently, I was stumped when I tried to del item of a list when iteration.

here is the wrong way I did:

lst = [1, 2, 3]
for i in lst:
print i
if i == 2:
lst.remove(i)

the result is:

1
2



如你所见,''缺少3''。这个问题是由''lst.remove(i)''引起的。



as you would see, ''3'' is missing. this problem is caused by ''lst.remove(i)''.




无论你是否删除东西,内部循环计数器都会增加,所以

当你通过移除中间的物品来移动物品时,循环

将跳过物品。这是基本的for-in用法,任何体面的教程都应该解释这个。

解决这个问题,你可以构建一个新的输出列表,循环向后或循环一份

副本。最后一个是最容易实现的:


lst = [1,2,3]

for i in lst [:]:

打印我

如果我= = 2:

lst.remove(i)


但其他人可能有优势在某些用例中。

如果你想了解更多细节,请参阅语言参考:

http://docs.python.org/ref/for.html


(注意警告)


< / F>



the internal loop counter is incremented whether you remove stuff or not, so
when you''re moving things around by removing items in the middle, the loop
will skip over items. this is basic for-in usage, and any decent tutorial should
explain this.
to fix this, you can build a new output list, loop backwards, or loop over a
copy. the last is easiest to implement:

lst = [1, 2, 3]
for i in lst[:]:
print i
if i == 2:
lst.remove(i)

but the others may have advantages in certain use cases.
if you want more details, see the language reference:

http://docs.python.org/ref/for.html

(note the warning)

</F>


Reinhold Birkenfeld写道:
Reinhold Birkenfeld wrote:
skull写道:
大家好,这是我在这个新闻组中的第一篇文章。
我是python的新手,虽然我有几年的c ++开发经验。
最近,我很难过当我在迭代时尝试del列表项时。

这是我做错的方法:

lst = [1,2,3]
for我在lst:
打印我
如果我== 2: lst.remove(i)

结果是:

1
2
Hi everybody, it is my first post in this newsgroup.
I am a newbie for python though I have several years development experience in c++.
recently, I was stumped when I tried to del item of a list when iteration.

here is the wrong way I did:

lst = [1, 2, 3]
for i in lst:
print i
if i == 2:
lst.remove(i)

the result is:

1
2
>


如你所见,3缺失。这个问题是由''lst.remove(i)''引起的。
显然,''标记和扫描''是解决这个问题的解决方案。
但我认为应该是更明智的伎俩。我想得到你的帮助。



as you would see, ''3'' is missing. this problem is caused by ''lst.remove(i)''.
apparently, ''marked-and-sweep'' is a solution to deal with this issue.
but I think there SHOULD BE more ''wise'' trick. I want to get your help.



快速解决方案:

我在lst [:]

迭代副本。



Quick solution:

for i in lst[:]

iterates over a copy.




另外:在Py2.4中,我无法找到问题


for i in reverse(lst )


有异议吗?


Reinhold



Addition: In Py2.4, I can''t find a problem with

for i in reversed(lst)

Any objections?

Reinhold


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

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