简体   繁体   English

将列表清空到新列表中

[英]Emptying a list into a new list

I'm trying to make a function that when a list is empty, a second list will empty into the empty one in reverse order. 我试图做一个函数,当一个列表为空时,第二个列表将以相反的顺序排入空列表。 Right now, I tried to do: 现在,我尝试执行以下操作:

a = [1,2,3,4]
b = []
def a_to_b(a, b):
    if not a:
        print('a:',a)        
        print('b:',b)
        for c in b:
            a.append(c)
            b.remove(c)
        print('a:',a)
        print('b:',b)
        return True
    else:
        top = a.pop()
        b.append(top)
        print('a:',a)
        print('b:',b)
        return True

I want it to be after each run: 我希望它在每次运行之后:

1) a = [1,2,3]
   b = [4]
2) a = [1,2]
   b = [4,3]
3) a = [1]
   b = [4,3,2]
4) a = []
   b = [4,3,2,1]
5) a = [1,2,3,4]
   b = []

But after this fifth run it is giving me 但是在第五次跑步之后,它给了我

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

And I cannot figure out why it is only applying to every other number in b. 而且我不知道为什么它只适用于b中的所有其他数字。

This should work 这应该工作

 a = [1,2,3,4] b = [] for i in range(len(a)): b.append(a[-1]) a.pop() print a,b 

Your problem is caused by you removing elements that you're hopping over in the for loop) 您的问题是由于您删除了要在for循环中跳过的元素而引起的)

Pointer is going as index 0, 1, 2, 3 but you're already removing the 0th element causing the pointer to go straight to 2 (which is now index 1 in remaining list) 指针将作为索引0、1、2、3,但是您已经删除了第0个元素,导致指针直接移至2(现在是剩余列表中的索引1)

to avoid it, you could change your code to: 为了避免这种情况,您可以将代码更改为:

for c in b:
    a.append(c)
for c in a:
    b.remove(c)

Here's the reason why you're getting a weird result: 这就是您得到奇怪结果的原因:

    for c in b:
        a.append(c)
        b.remove(c)

You're changing list b as you're iterating over it. 您正在遍历列表b时对其进行更改。 Thus things are not going to come out as you expect them to. 因此,事情不会像您期望的那样出现。 Why don't you just do 你为什么不做

b.reverse()
a = b
b = []

In the place of what you had before. 取代您以前拥有的东西。 So it'd be 所以会

a = [1,2,3,4]
b = []
def a_to_b(a, b):
    if not a:
        print('a:',a)        
        print('b:',b)
        b.reverse()
        a = b
        b = []
        print('a:',a)
        print('b:',b)
        return True
    else:
        top = a.pop()
        b.append(top)
        print('a:',a)
        print('b:',b)
        return True
def f(a, b):
    if a:
        b.append(a.pop())
    else:
        while b:
            a.append(b.pop())
    print 'a: %s' % a
    print 'b: %s' % b

>>> a = [1, 2, 3, 4]
>>> b = []
>>> f(a, b)
a: [1, 2, 3]
b: [4]

>>> f(a, b)
a: [1, 2]
b: [4, 3]

>>> f(a, b)
a: [1]
b: [4, 3, 2]

>>> f(a, b)
a: []
b: [4, 3, 2, 1]

>>> f(a, b)
a: [1, 2, 3, 4]
b: []

The problem is here: 问题在这里:

for c in b:
        a.append(c)
        b.remove(c)

You cant remove objects from the middle of an interable without confusing the loop. 您不能从互感器的中间删除对象,而不会混淆循环。

Heres what happens: 继承人发生了什么:

You have b=[4,3,2,1] and a=[] . 您有b=[4,3,2,1]a=[] You start the for loop and c is pointing to the first index of b, ie 4. You remove c from the list and put it in a . 您开始for循环,并且c指向b的第一个索引,即4。从列表中删除c并将其放在a

Now you have b = [3,2,1] and a=[4] like you expect. 现在,如您所料,您有b = [3,2,1]a=[4]

When you try to start the next loop, your index is incremented ( c is now pointing at the 2nd element) but the problem is you've messed with the structure of the iterable. 当您尝试开始下一个循环时,索引会递增( c现在指向第二个元素),但是问题是您已经弄乱了iterable的结构。 So the loop removes c like its supposed to, but c=2 , not 3 like you are expecting. 因此,循环会像预期的那样删除c ,但是c=2 ,而不是您期望的3。

Now you have a=[4,2] and b=[1,3] and when the loop checks for index 3, it finds that b is only has 2 elements so it exits. 现在您有了a=[4,2]b=[1,3] ,当循环检查索引3时,它发现b只有2个元素,因此退出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM