How to change items in a sublist?

I am trying to create a function that changes the order of items in a list, and also overrides items in a sublist. eg:

For example, if L = [[1, 2], [3, 4], [5, 6, 7]], then deep_reverse (L) mutates L as [[7, 6, 5], [4, 3], [2, 1]]

I figured out how to reorder a single list, but I am having problems reordering items in a sublist. This is what I have so far:

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)

In the above example, my code will simply print [5,6,7], [3,4], [1,2], which I am not trying to execute. This simply reverses the order of the lists, not the actual elements in the lists.

What should I add to the code so that it also changes the order of elements in the sublist?

[ EDIT: ; , , .]

+4
7

:). , :

, , - ( , , L).

, -:

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 . item -, .

L, , range(len()), .

for i, item in enumerate(L):
    # do something with L[i]
    L[i] = 'something'

: , Stefan Pochmann :

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

, return . L . L , L L. list.reverse() L , .

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

. [::-1] reversed, .

EDIT:

, reversed . listreverseiterator

:

, :

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

Edit:

:

L[:] = new_list 

.

+4

, , .

:

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

.

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
+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.

+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.
    """
    # Your code here
    for i in L:
        try:
            deep_reverse(i)
        except:
            pass
    L.reverse()
0

, .

- (UNTESTED):

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)
-1

Source: https://habr.com/ru/post/1656530/


All Articles