2

我有一个嵌套列表,我需要反转列表中的每个元素。但我不知道该列表是否是列表列表的列表。所以例子是:

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]

到目前为止,我所拥有的是:

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

它适用于第二个测试,即 q 测试,但不适用于第一个测试。我不确定我是否需要使用递归来循环整个事情?如何修改我的代码?谢谢。

4

10 回答 10

4
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
于 2012-06-02T01:35:39.243 回答
4

您的代码不起作用的原因是因为如果i是列表列表,则您deep_reverse不在i.

您只需将一行代码更改为以下内容:

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
于 2012-06-02T01:37:58.287 回答
1
>>> 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]
于 2012-06-02T06:21:56.283 回答
1

这是我的解决方案

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])]
        
于 2013-01-17T19:05:51.127 回答
1
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]

希望这可以帮助

于 2012-06-02T01:48:45.657 回答
0
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])]

测试确定。

于 2013-04-05T09:12:17.317 回答
0

这是一个建议:

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
于 2012-12-03T17:34:59.730 回答
0
def deep_reverse(ls):  
    for i in ls:  
        if type(i)==list:deep_reverse(i)  
    ls.reverse()  
于 2012-06-02T04:43:43.053 回答
0

最优雅:

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()
于 2017-09-16T17:42:40.627 回答
0

我在这个问题的屁股上上学:

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])
于 2021-09-17T03:01:08.243 回答