问题描述:

un_confirmusers = ['A','B','C','D','E']
confirmusers = []
for un_confirmuser in un_confirmusers:
    current_user = un_confirmusers.pop()
    print("The current USER is: " + current_user.title())
    confirmusers.append(current_user)
print(confirmusers)
print(un_confirmusers)


编译结果:

The current USER is: E
The current USER is: D
The current USER is: C
['E', 'D', 'C']
['A', 'B']


我不知道pop()不能完全弹出列表中位数。
谢谢。

最佳答案

除了使用for-loop,还可以使用while-loop

例如:

un_confirmusers = ['A','B','C','D','E']
confirmusers = []
while un_confirmusers:                      #!Update.
    current_user = un_confirmusers.pop()
    print("The current USER is: " + current_user.title())
    confirmusers.append(current_user)
print(confirmusers)
print(un_confirmusers)


输出:

The current USER is: E
The current USER is: D
The current USER is: C
The current USER is: B
The current USER is: A
['E', 'D', 'C', 'B', 'A']
[]


注意:在迭代对象时删除元素不是一个好习惯。

关于python - pop()函数不能完全弹出列表中位数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54995564/

10-11 08:05
查看更多