列出问题 [英] List problem

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

问题描述

你好,


i对以下代码有疑问。

test_list = [1,2,3]

我在test_list中获得


打印i


如果在test_list中为1:

test_list.remove(1)

为什么不打印第二项?

这是一个错误还是一个功能?!

谢谢,

Thomas

Hello,

i have a question on the following code.
test_list = [1, 2, 3]

for i in test_list:
print i

if 1 in test_list:
test_list.remove(1)
Why is the second item not print ?
Is that a bug or a feature ?!
Thanks,
Thomas

推荐答案

Thomas M.< thomas.sunshine< at> web.de>写道:
Thomas M. <thomas.sunshine <at> web.de> writes:

test_list = [1,2,3]

我在test_list中:
打印我

如果在test_list中为1:
test_list.remove(1)

为什么第二项不能打印?

test_list = [1, 2, 3]

for i in test_list:
print i

if 1 in test_list:
test_list.remove(1)

Why is the second item not print ?




也许这会帮助说明问题:



Maybe this will help illustrate the problem:

test_list = [1,2,3]
for i,item在枚举(test_list):
....打印项目

....打印"开始",i,test_list [i],test_list

....如果在test_list中为1:

.... test_list.remove(1)

.... print" end",i,test_list [i ],test_list

....

1

start 0 1 [1,2,3]

结束0 2 [2,3]

3

开始1 3 [2,3]

结束1 3 [2,3]


在循环的第一次迭代中,您正在查看3项目列表中的项目0,

[1,2,3]。当您从列表中删除1时,将3项列表转换为

2项列表,将索引向下移动,以便test_list [0]现在为2.

当for循环继续时,它现在正在查看索引1,并且(调整后的)2项目列表中索引

1的项目是3.


一般情况下,在迭代列表时从列表中删除元素

是个坏主意。也许更好的解决方案是列表理解:

test_list = [1,2,3]
[x for test_list中x如果x!= 1]
[2,3] ]对于[x for test inlist in x!= 1]中的项目:
test_list = [1, 2, 3]
for i, item in enumerate(test_list): .... print item
.... print "start", i, test_list[i], test_list
.... if 1 in test_list:
.... test_list.remove(1)
.... print "end", i, test_list[i], test_list
....
1
start 0 1 [1, 2, 3]
end 0 2 [2, 3]
3
start 1 3 [2, 3]
end 1 3 [2, 3]

On the first iteration of the loop, you are looking at item 0 in a 3-item list,
[1, 2, 3]. When you remove 1 from the list, you convert the 3-item list into a
2-item list, thust shifting the indices down, so that test_list[0] is now 2.
When the for loop continues, it is now looking at index 1, and the item at index
1 in your (adjusted) 2-item list is 3.

In general, removing elements from a list while you''re iterating though the list
is a bad idea. Perhaps a better solution is a list comprehension:
test_list = [1, 2, 3]
[x for x in test_list if x != 1] [2, 3] for item in [x for x in test_list if x != 1]:



....打印项目

....

2

3


史蒂夫


.... print item
....
2
3

Steve


2004年10月29日星期五17:52:03 +0000(UTC),Steven Bethard

< st ************ @ gmail。 COM>写道:
On Fri, 29 Oct 2004 17:52:03 +0000 (UTC), Steven Bethard
<st************@gmail.com> wrote:
Thomas M.< thomas.sunshine< at> web.de>写道:
Thomas M. <thomas.sunshine <at> web.de> writes:

test_list = [1,2,3]

我在test_list中:
打印我

如果在test_list中为1:
test_list.remove(1)

为什么第二项不打印?
也许这有助于说明问题:

test_list = [1, 2, 3]

for i in test_list:
print i

if 1 in test_list:
test_list.remove(1)

Why is the second item not print ?
Maybe this will help illustrate the problem:
test_list = [1,2,3]
对于i,枚举项目(test_list):... print item
... print" ; start",i,test_list [i],test_list
...如果在test_list中为1:
... test_list.remove(1)
... print" end",我,test_list [i],test_list
......
1
开始0 1 [1,2,3]
结束0 2 [2,3]
3
start 1 3 [2,3]
end 1 3 [2,3]

在循环的第一次迭代中,您正在查看3中的项目0 -item list,
[1,2,3]。当您从列表中删除1时,将3项列表转换为
2项列表,将索引向下移动,以便test_list [0]现在为2.
当for循环时继续,现在查看索引1,并且(调整后的)2项目列表中索引
1处的项目为3.

一般情况下,当您在列表中删除元素时虽然在列表中重复,但这是一个坏主意。也许更好的解决方案是列表理解:
test_list = [1, 2, 3]
for i, item in enumerate(test_list):... print item
... print "start", i, test_list[i], test_list
... if 1 in test_list:
... test_list.remove(1)
... print "end", i, test_list[i], test_list
...
1
start 0 1 [1, 2, 3]
end 0 2 [2, 3]
3
start 1 3 [2, 3]
end 1 3 [2, 3]

On the first iteration of the loop, you are looking at item 0 in a 3-item list,
[1, 2, 3]. When you remove 1 from the list, you convert the 3-item list into a
2-item list, thust shifting the indices down, so that test_list[0] is now 2.
When the for loop continues, it is now looking at index 1, and the item at index
1 in your (adjusted) 2-item list is 3.

In general, removing elements from a list while you''re iterating though the list
is a bad idea. Perhaps a better solution is a list comprehension:




除非你使用while循环并反向迭代,否则

示例:


a = [0,1,2,3,4]

pos_max = len(a) - 1#最大可迭代元素

pos = pos_max#当前元素

而pos> = 0:

如果[pos] == 2:

a。删除(2)

pos_max = pos_max - 1

pos = pos - 1

test_list = [1,2,3 ]
[x for test inlist if x!= 1] [2,3] for item in [x for x in test_list if x!= 1]:



Unless you''re using a while loop and iterating in reverse, for
example:

a = [0,1,2,3,4]
pos_max = len(a) - 1 # Maximum iterable element
pos = pos_max # Current element
while pos >= 0:
if a[pos] == 2:
a.remove(2)
pos_max = pos_max - 1
pos = pos - 1

test_list = [1, 2, 3]
[x for x in test_list if x != 1][2, 3] for item in [x for x in test_list if x != 1]:


...打印项目
...
2
史蒂夫


... print item
...
2
3

Steve





User< 1@2.3>写道:

...
User <1@2.3> wrote:
...
一般情况下,在迭代时从列表中删除元素
列表是个坏主意。或许更好的解决方案是列表
理解:
In general, removing elements from a list while you''re iterating though
the list is a bad idea. Perhaps a better solution is a list
comprehension:



除非你使用while循环并反向迭代,否则
例如:

a = [0,1,2,3,4]
pos_max = len(a) - 1#最大可迭代元素
pos = pos_max#当前元素
而pos> ; = 0:
如果[pos] == 2:
a.remove(2)
pos_max = pos_max - 1
pos = pos - 1



Unless you''re using a while loop and iterating in reverse, for
example:

a = [0,1,2,3,4]
pos_max = len(a) - 1 # Maximum iterable element
pos = pos_max # Current element
while pos >= 0:
if a[pos] == 2:
a.remove(2)
pos_max = pos_max - 1
pos = pos - 1




还是一个坏主意。


a [:] = [x代表x中的x!= 2]

/>
更简洁,惯用,更快捷。没有理由做低级别的b / b
用指数和繁琐的循环,如果列表

理解能够如此简单地完成工作。

Alex



Still a bad idea.

a[:] = [x for x in a if x != 2]

is more concise, idiomatic, and speedy. No reason to do low-level
twiddling with indices and fiddly loops, in cases where a list
comprehension can do the job so simply.
Alex


这篇关于列出问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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