0
    x = []
    for i in l[0::3]:
        x.append(i)
    for j in l[1::3]:
        x.append(j)
    for k in l[2::3]:
        x.append(k)
    print(x)

I want to take a random list like

[1, 2, 3, 4, 5, 6, 7] 

that would return

[1, 4, 7, 2, 5, 4, 7]

but for a list of any number. is there a way to increase the start by 1 ?

4

1 回答 1

1

使用列表理解:

>>> xs = [1, 2, 3, 4, 5, 6, 7]
>>> [x for i in range(3) for x in xs[i::3]]
[1, 4, 7, 2, 5, 3, 6]
于 2013-10-02T02:28:09.917 回答