0
def is_list(p):

    return isinstance(p, list)


def deep_reverse(list):

    o=[]
    for i in reversed(list):

        if is_list(i)==True:
            print i
            deep_reverse(i)

        o.append(i)

    return o

例如:

p = [1, [2, 3, [4, [5, 6]]]]
print deep_reverse(p)
#>>> [[[[6, 5], 4], 3, 2], 1]
4

1 回答 1

7

换行

        deep_reverse(i)

        i = deep_reverse(i)

顺便说一句,编写此函数的更短的方法是:

def deep_reverse(lst):
    if not is_list(lst):
        return lst
    return map(deep_reverse, reversed(lst))
于 2013-01-25T18:57:55.853 回答