pre-exam_exercise2

def rearrange(L,from_first=True):
    new_list=[]
    if from_first:
        for i in range(int(len(L)/2)):
            new_list.append(L[i])
            new_list.append(L[-i-1])
        if len(L)%2!=0:
            new_list.append(L[int(len(L)/2)])
    else:
        for i in range(int(len(L)/2)):
            new_list.append(L[-i-1])
            new_list.append(L[i])
        if len(L)%2!=0:
            new_list.append(L[int(len(L)/2)])
    return new_list

answer=rearrange(L=[10,20,30,40,50],from_first=False)
print(answer)

Returns a new list consisting of:

* in case "from_first" is True:

L’s first member if it exists, then L’s last member if it exists, then L’s second member if it exists, then L’s second last member if it exists, then L’s third member if it exists...

* in case "from_first" is False:

L’s last member if it exists, then L’s first member if it exists, then L’s second last member if it exists, then L’s second member if it exists, then L’s third last member if it exists...

原文地址:https://www.cnblogs.com/eleni/p/11251455.html