Reputation: 89
I have a nested list, and I need to reverse every element in the list. But I dont know whether the list is a list of list of list or not. So example is:
p = [1, [2, 3, [4, [5, 6]]]]
print deep_reverse(p)
#>>> [[[[6, 5], 4], 3, 2], 1]
q = [1, [2,3], 4, [5,6]]
print deep_reverse(q)
#>>> [ [6,5], 4, [3, 2], 1]
What I have so far is:
def is_list(p):
return isinstance(p, list)
def deep_reverse(a):
a.reverse()
for i in a:
if is_list(i):
i.reverse()
print a
It works well for the second test, the q one, but doest work for the first test. I am not sure do I need use a recursion to loop the whole thing? How can I modify my code? Thanks.
Upvotes: 2
Views: 8801
Reputation: 1
I am going old school on this problem's butt:
def deepReverse(L):
"""Input a list tot output a reversed form of it"""
if not L:
return []
elif isinstance(L[-1], list):
return [deepReverse(L[-1])] + deepReverse(L[0:-1])
else:
return [L[-1]] + deepReverse(L[0:-1])
Upvotes: 0
Reputation: 13
Here is my solution
def is_list(p):
return isinstance(p, list)
def deep_reverse(p):
if p==[]:
return p
if not is_list(p[0]):
return deep_reverse(p[1:])+[p[0]]
else:
return deep_reverse(p[1:])+[deep_reverse(p[0])]
Upvotes: 1
Reputation: 112
most elegant:
def deep_reverse(L):
""" assumes L is a list of lists whose elements are ints
Mutates L such that it reverses its elements and also
reverses the order of the int elements in every element of L.
It does not return anything.
"""
# Your code here
for i in L:
try:
deep_reverse(i)
except:
pass
L.reverse()
Upvotes: 0
Reputation: 3534
def is_list(p):
return isinstance(p, list)
def deep_reverse(p):
if p==[]:
return p
if not is_list(p):
return p
else:
return deep_reverse(p[1:])+[deep_reverse(p[0])]
test ok.
Upvotes: 0
Reputation: 1895
here is a suggestion :
def deep_reverse(lst):
if isinstance(lst ,list):
if sum(1 for x in lst if isinstance(x, list)) == 0:
lst = lst[::-1]
return lst
else :
lst = lst[::-1]
lst = [deep_reverse(item) for item in lst]
return lst
else:
return lst
Upvotes: 0
Reputation: 134811
def deep_reverse(lst):
try:
if len(lst) > 1:
return list(deep_reverse(item) for item in reversed(lst))
return lst
except TypeError:
return lst
Upvotes: 4
Reputation: 133504
>>> def deep_reverse(L):
for item in reversed(L):
if isinstance(item,list):
yield list(deep_reverse(item))
else:
yield item
>>> p = [1, [2, 3, [4, [5, 6]]]]
>>> q = [1, [2,3], 4, [5,6]]
>>> list(deep_reverse(p))
[[[[6, 5], 4], 3, 2], 1]
>>> list(deep_reverse(q))
[[6, 5], 4, [3, 2], 1]
Upvotes: 1
Reputation: 2750
def deep_reverse(ls):
for i in ls:
if type(i)==list:deep_reverse(i)
ls.reverse()
Upvotes: 0
Reputation: 113905
def deep_reverse(L):
if L == []:
return L
elif type(L) == int:
return L
else:
return deep_reverse(L[1:]) + [deep_reverse(L[0])]
>>> print deep_reverse(p)
[[[[6, 5], 4], 3, 2], 1]
>>> print deep_reverse(q)
[[6, 5], 4, [3, 2], 1]
Hope this helps
Upvotes: 1
Reputation: 245
The reason your code doesn't work is because if i
is a list of lists, you don't deep_reverse
the lists within i
.
You only need to change one line of your code to the following:
def is_list(p):
return isinstance(p, list)
def deep_reverse(a):
a.reverse()
for i in a:
if is_list(i):
deep_reverse(i) # <=== This is what changed
print a
Upvotes: 4