Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x pop()和enumerate()如何交互的问题_Python 3.x_Stack_Enumerate - Fatal编程技术网

Python 3.x pop()和enumerate()如何交互的问题

Python 3.x pop()和enumerate()如何交互的问题,python-3.x,stack,enumerate,Python 3.x,Stack,Enumerate,在使用enumerate的for循环中使用List.pop时,进程在到达列表末尾之前终止。我怎样才能避免让流行音乐打断这个过程 我已经检查过,如果我使用pop编写循环,但不使用enumerate,那么它将按预期工作。类似地,如果我删除pop并使用enumerate执行其他操作,它也会按预期工作 代码如下: x=[0,2,4,6,8,10] for i in enumerate(x): x.pop(0) print(x) 我希望打印以下内容: [2,4,6,8,10] [4,6,8,10] [

在使用enumerate的for循环中使用List.pop时,进程在到达列表末尾之前终止。我怎样才能避免让流行音乐打断这个过程

我已经检查过,如果我使用pop编写循环,但不使用enumerate,那么它将按预期工作。类似地,如果我删除pop并使用enumerate执行其他操作,它也会按预期工作

代码如下:

x=[0,2,4,6,8,10] 
for i in enumerate(x):
x.pop(0)
print(x)
我希望打印以下内容:

[2,4,6,8,10]
[4,6,8,10]
[6,8,10]
[8,10]
[10]
[]
相反,我收到了

[2,4,6,8,10]
[4,6,8,10]
[6,8,10]
如果我再次运行它,那么我将收到

[8,10]
[10]
[]
如果我再次运行它,我将收到

[8,10]
[10]
[]

使用
范围

Ex:

x=[0,2,4,6,8,10] 
for i in range(len(x)):
    x.pop(0)
    print(x)
[2, 4, 6, 8, 10]
[4, 6, 8, 10]
[6, 8, 10]
[8, 10]
[10]
[]
输出:

x=[0,2,4,6,8,10] 
for i in range(len(x)):
    x.pop(0)
    print(x)
[2, 4, 6, 8, 10]
[4, 6, 8, 10]
[6, 8, 10]
[8, 10]
[10]
[]

注意:在迭代时修改列表不是一种好的做法

谢谢你的回答。你能解释为什么len(x)在x被修改的意义上没有问题吗?我同意使用您建议的版本,但我想了解枚举版本不起作用的原因。