Python 如何删除列表中的所有奇数序号项?

Python 如何删除列表中的所有奇数序号项?,python,list,Python,List,我在stackoverflow上搜索过,看到有人写了这样的话: [item for item in yourlist if item % 2] 这对我不起作用 我的代码如下: x = ['apple','fruit','orange','fruit','lemon','fruit'] for i in range(0,len(x),2): if i%2 !=0: x.pop(i) print x 这也不行。它说流行指数超出范围 如何操作?大步使用切片: x = x[1

我在stackoverflow上搜索过,看到有人写了这样的话:

[item for item in yourlist if item % 2]
这对我不起作用

我的代码如下:

x = ['apple','fruit','orange','fruit','lemon','fruit']
for i in range(0,len(x),2):
    if i%2 !=0:
        x.pop(i)
print x
这也不行。它说流行指数超出范围


如何操作?

大步使用切片:

x = x[1::2]
或选择奇数项而不是偶数项:

x = x[::2]
第一个从第二个项目开始,获取输入列表中的每一个第二个元素。另一个从列表中的第一项开始,每隔一秒提取一个元素

演示:

原始代码仅在希望仅选择偶数值而不是偶数索引时才起作用。可以使用向该循环添加索引:

>>> [f for i, f in enumerate(x) if i % 2]
['fruit', 'fruit', 'fruit']

但是在这里切片要容易得多。

请大步使用切片:

x = x[1::2]
或选择奇数项而不是偶数项:

x = x[::2]
第一个从第二个项目开始,获取输入列表中的每一个第二个元素。另一个从列表中的第一项开始,每隔一秒提取一个元素

演示:

原始代码仅在希望仅选择偶数值而不是偶数索引时才起作用。可以使用向该循环添加索引:

>>> [f for i, f in enumerate(x) if i % 2]
['fruit', 'fruit', 'fruit']

但是切片在这里要容易得多。

您的代码失败了,因为您试图修改正在迭代的列表,而没有考虑副作用

x = ['apple','fruit','orange','fruit','lemon','fruit']
for i in range(0,len(x),2):
    if i%2 !=0:
        x.pop(i)
print x
注1:
范围(0,len(x),2)
将产生
[0,2,4]
,并且所有这些都不会在该条件下成功。所以,我假设你的意思是
范围(1,len(x),2)

注2:由于您正在迭代
范围(1,len(x),2)
,实际上是
[1,3,5]
,因此
if
条件已过时

i
为1时,我们将元素弹出到1。所以实际发生的是

在1处弹出元素之前

在1处弹出元素后

同样,当
i
变为3时,我们将元素弹出到3

在3处弹出元素之前

在3处弹出元素后

现在,
i
变为5,并且在位置5处没有元素。这就是为什么
x.pop(5)

pop index out of range
错误。你可以用这个程序来确认这一点

x = ['apple','fruit','orange','fruit','lemon','fruit']
try:
    for i in range(1,len(x),2):
        if i%2 !=0:
            x.pop(i)
except IndexError, e:
    print e, x, i
输出是这样的

pop index out of range ['apple', 'orange', 'fruit', 'fruit'] 5
print x[::2]    # ['apple', 'orange', 'lemon']
print x[1::2]   # ['fruit', 'fruit', 'fruit']
x = ['apple','fruit','orange','fruit','lemon','fruit']
print [item for idx, item in enumerate(x) if idx % 2 == 0]
# ['apple', 'orange', 'lemon']
print [item for idx, item in enumerate(x) if idx % 2 == 1]
# ['fruit', 'fruit', 'fruit']
解决方案

您可以使用切片表示法仅获取偶数顺序数据中的元素,如下所示

pop index out of range ['apple', 'orange', 'fruit', 'fruit'] 5
print x[::2]    # ['apple', 'orange', 'lemon']
print x[1::2]   # ['fruit', 'fruit', 'fruit']
x = ['apple','fruit','orange','fruit','lemon','fruit']
print [item for idx, item in enumerate(x) if idx % 2 == 0]
# ['apple', 'orange', 'lemon']
print [item for idx, item in enumerate(x) if idx % 2 == 1]
# ['fruit', 'fruit', 'fruit']
你可以得到像这样的奇数序数的元素

pop index out of range ['apple', 'orange', 'fruit', 'fruit'] 5
print x[::2]    # ['apple', 'orange', 'lemon']
print x[1::2]   # ['fruit', 'fruit', 'fruit']
x = ['apple','fruit','orange','fruit','lemon','fruit']
print [item for idx, item in enumerate(x) if idx % 2 == 0]
# ['apple', 'orange', 'lemon']
print [item for idx, item in enumerate(x) if idx % 2 == 1]
# ['fruit', 'fruit', 'fruit']
否则,您可以使用列表理解来过滤掉奇数顺序数据,如下所示

pop index out of range ['apple', 'orange', 'fruit', 'fruit'] 5
print x[::2]    # ['apple', 'orange', 'lemon']
print x[1::2]   # ['fruit', 'fruit', 'fruit']
x = ['apple','fruit','orange','fruit','lemon','fruit']
print [item for idx, item in enumerate(x) if idx % 2 == 0]
# ['apple', 'orange', 'lemon']
print [item for idx, item in enumerate(x) if idx % 2 == 1]
# ['fruit', 'fruit', 'fruit']

您的代码失败了,因为您试图修改正在迭代的列表,而没有考虑副作用

x = ['apple','fruit','orange','fruit','lemon','fruit']
for i in range(0,len(x),2):
    if i%2 !=0:
        x.pop(i)
print x
注1:
范围(0,len(x),2)
将产生
[0,2,4]
,并且所有这些都不会在该条件下成功。所以,我假设你的意思是
范围(1,len(x),2)

注2:由于您正在迭代
范围(1,len(x),2)
,实际上是
[1,3,5]
,因此
if
条件已过时

i
为1时,我们将元素弹出到1。所以实际发生的是

在1处弹出元素之前

在1处弹出元素后

同样,当
i
变为3时,我们将元素弹出到3

在3处弹出元素之前

在3处弹出元素后

现在,
i
变为5,并且在位置5处没有元素。这就是为什么
x.pop(5)

pop index out of range
错误。你可以用这个程序来确认这一点

x = ['apple','fruit','orange','fruit','lemon','fruit']
try:
    for i in range(1,len(x),2):
        if i%2 !=0:
            x.pop(i)
except IndexError, e:
    print e, x, i
输出是这样的

pop index out of range ['apple', 'orange', 'fruit', 'fruit'] 5
print x[::2]    # ['apple', 'orange', 'lemon']
print x[1::2]   # ['fruit', 'fruit', 'fruit']
x = ['apple','fruit','orange','fruit','lemon','fruit']
print [item for idx, item in enumerate(x) if idx % 2 == 0]
# ['apple', 'orange', 'lemon']
print [item for idx, item in enumerate(x) if idx % 2 == 1]
# ['fruit', 'fruit', 'fruit']
解决方案

您可以使用切片表示法仅获取偶数顺序数据中的元素,如下所示

pop index out of range ['apple', 'orange', 'fruit', 'fruit'] 5
print x[::2]    # ['apple', 'orange', 'lemon']
print x[1::2]   # ['fruit', 'fruit', 'fruit']
x = ['apple','fruit','orange','fruit','lemon','fruit']
print [item for idx, item in enumerate(x) if idx % 2 == 0]
# ['apple', 'orange', 'lemon']
print [item for idx, item in enumerate(x) if idx % 2 == 1]
# ['fruit', 'fruit', 'fruit']
你可以得到像这样的奇数序数的元素

pop index out of range ['apple', 'orange', 'fruit', 'fruit'] 5
print x[::2]    # ['apple', 'orange', 'lemon']
print x[1::2]   # ['fruit', 'fruit', 'fruit']
x = ['apple','fruit','orange','fruit','lemon','fruit']
print [item for idx, item in enumerate(x) if idx % 2 == 0]
# ['apple', 'orange', 'lemon']
print [item for idx, item in enumerate(x) if idx % 2 == 1]
# ['fruit', 'fruit', 'fruit']
否则,您可以使用列表理解来过滤掉奇数顺序数据,如下所示

pop index out of range ['apple', 'orange', 'fruit', 'fruit'] 5
print x[::2]    # ['apple', 'orange', 'lemon']
print x[1::2]   # ['fruit', 'fruit', 'fruit']
x = ['apple','fruit','orange','fruit','lemon','fruit']
print [item for idx, item in enumerate(x) if idx % 2 == 0]
# ['apple', 'orange', 'lemon']
print [item for idx, item in enumerate(x) if idx % 2 == 1]
# ['fruit', 'fruit', 'fruit']

您在遍历
列表时正在对其进行变异。不要这样做。当您在列表上迭代时,项目的索引会发生更改@我知道每次迭代时,除item0之外的每个项目的序号都会发生更改,但我一开始无法找到其他方法。您在迭代列表时会对列表进行变异。不要这样做。当您在列表上迭代时,项目的索引会发生更改@I know每次迭代时,除了item0之外,每个项目的序号都会发生更改,但我一开始无法找到其他方法。