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]
答案 0 :(得分: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))