如何反转子列表中的元素?

时间:2016-10-02 20:06:55

标签: python list python-3.x reverse sublist

我试图创建一个反转列表中元素顺序的函数,并且还反转子列表中的元素。例如:

例如,如果L = [[1,2],[3,4],[5,6,7]]则deep_reverse(L)将L变为[[7,6,5],[4] ,3],[2,1]]

我想出了如何颠倒一个列表的顺序,但是我在扭转子列表中元素的顺序时遇到了麻烦。这就是我到目前为止所做的:

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.
    """
    for i in reversed(L):
          print(i)

在上面的例子中,我的代码只会打印[5,6,7], [3,4], [1,2],这不是我想要完成的。它只是颠倒了列表的顺序,而不是列表中的实际元素。

我应该在代码中添加什么,以便它还反转子列表中元素的顺序?

[编辑:我的代码需要来改变列表;我不想只打印它,它实际上需要更改列表。]

7 个答案:

答案 0 :(得分:4)

[sublist[::-1] for sublist in to_reverse[::-1]]

列表理解在这里工作。 [::-1]reversed基本相同,但不会修改列表。

编辑:

如下所述,reversed不会修改列表。它返回一个listreverseiterator对象

更多编辑:

如果您想要任意深度列表的解决方案,请尝试:

def deep_reverse(to_reverse):
    if isinstance(to_reverse, list):
        return list(map(deep_reverse, to_reverse[::-1]))
    else:
        return to_reverse

更多编辑:

改变函数中的列表:

L[:] = new_list 

将修改列表。

答案 1 :(得分:2)

  

我试图创建一个反转列表中元素顺序的函数,并且还反转子列表中的元素。

然后完成这两件事:

L.reverse()
for sublist in L:
    sublist.reverse()

完整演示,因为您似乎对您的功能应该做什么以及如何测试它感到困惑:

>>> 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.
        """
        L.reverse()
        for sublist in L:
            sublist.reverse()

>>> L = [[1, 2], [3, 4], [5, 6, 7]]
>>> deep_reverse(L)
>>> print(L)
[[7, 6, 5], [4, 3], [2, 1]]

答案 2 :(得分:1)

这应该可以解决问题。

L = [[1, 2], [3, 4], [5, 6, 7]]

def deep_reverse(L):
    for i in range(len(L)):
        L[i]=L[i][::-1]
    L=L[::-1]
    return L

答案 3 :(得分:1)

或者,您可以使用map()来实现此目的:

>>> map(lambda x: x[::-1], L[::-1])       # In Python 2.x
[[7, 6, 5], [4, 3], [2, 1]]

>>> list(map(lambda x: x[::-1], L[::-1])) # In Python 3.x
[[7, 6, 5], [4, 3], [2, 1]]

检查Lambda, filter, reduce and map上的博客,了解lambda函数和map()如何在Python中运行。

答案 4 :(得分:0)

这看起来很熟悉:)。我不打算提供整个解决方案,但这里有一些提示:

如您所知,有两个步骤,反转每个子列表,然后反转外部列表(就地,不创建新列表,因此它将改变全局for i in range(len(L)): # if L[i] is a list: # reverse with [::-1] and update L[i] to the reversed version # reverse the outer list L, list.reverse() will operate in-place on L )。

所以你可以循环遍历外部列表,并改变每个子列表:

for item in list:
    item = 'xxx'

现在请记住,如果你循环遍历这个列表:

item

您无法使用上述代码更改itemL是占位符值,因此更改它并不会实际修改列表。

您需要在range(len())中对该项目编制索引,并且枚举可以对此有所帮助,或者您可以使用上述较少提供的for i, item in enumerate(L): # do something with L[i] L[i] = 'something'

def deep_reverse(L):
    L.reverse()
    for sublist in L:
        sublist.reverse()

编辑:由于对此存在很多困惑,我将继续发布一个基于Stefan Pochmann的工作解决方案,这是一个非常优雅的答案:

L

请注意,没有返回语句,没有打印语句。这将正确修改L。您无法在函数内重新分配L,因为它只会创建L的新本地版本,并且不会修改全局list.reverse()。您可以使用L根据规范修改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. """ for i in reversed(L): if len(i) > 1: deep_reverse(i) else: print(i) 到位

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

答案 6 :(得分:-1)

你可以使这个递归,所以它适用于任意深度的巢。

类似于(UNTESTED):

pycharm
相关问题