Skipping every other element after the first

后端 未结 13 2207
深忆病人
深忆病人 2020-12-04 21:13

I have the general idea of how to do this in Java, but I am learning Python and not sure how to do it.

I need to implement a function that returns a list containing

13条回答
  •  没有蜡笔的小新
    2020-12-04 21:55

    Using the for-loop like you have, one way is this:

    def altElement(a):
        b = []
        j = False
        for i in a:
            j = not j
            if j:
                b.append(i)
    
        print b
    

    j just keeps switching between 0 and 1 to keep track of when to append an element to b.

提交回复
热议问题