How do I reverse a sublist in a list in place?

前端 未结 12 2575
-上瘾入骨i
-上瘾入骨i 2021-02-20 01:24

I\'m supposed to create a function, which input is a list and two numbers, the function reverses the sublist which its place is indicated by the two numbers. for example this is

相关标签:
12条回答
  • 2021-02-20 02:02

    Just use a slice:

    >>> lst = [1, 2, 3, 4, 5] 
    >>> lst[0:len(lst[3::-1])]=lst[3::-1]
    >>> lst
    [4, 3, 2, 1, 5]
    

    Or, perhaps easier to understand:

    >>> lst = [1, 2, 3, 4, 5] 
    >>> sl=lst[3::-1]
    >>> lst[0:len(sl)]=sl
    >>> lst
    [4, 3, 2, 1, 5]
    
    0 讨论(0)
  • 2021-02-20 02:06

    I've conducted a tiny experiment and it seems that any assignment to list slice causes memory allocation:

    import resource
    resource.setrlimit(resource.RLIMIT_AS, (64 * 1024, 64 * 1024))
    
    try:
        # Python 2
        zrange = xrange
        arr_size = 3 * 1024
    except NameError:
        # Python 3
        zrange = range
        arr_size = 4 * 1024
    
    arr = list(zrange(arr_size))
    
    # We could allocate additional 100 integers, so there should be enough free memory
    # to allocate a couple of variables for indexes in the statement below
    #   additional_memory = list(zrange(100))
    
    # MemoryError is raised here
    arr[:] = zrange(arr_size)
    

    So you have to use for loop to reverse a sublist in place.

    PS: If you want to repeat this test, you should ensure that setrlimit RLIMIT_AS works fine on your platform. Also arr_size may vary for different python implementations.

    0 讨论(0)
  • 2021-02-20 02:07

    Try some crazy slicing, see Explain Python's slice notation and http://docs.python.org/2.3/whatsnew/section-slices.html

    x = [1,2,3,4,5,6,7,8]
    
    def sublist_reverse(start_rev, end_rev, lst):
        return lst[:end_rev-1:start_rev-1]+lst[:[end_rev]
    
    print sublist_reverse(0,4,x)
    

    [out]:

    [8, 7, 6, 5, 4, 3, 2, 1]
    
    0 讨论(0)
  • 2021-02-20 02:08
    lst = [1,2,3,4,5,6,7,8]
    

    Suppose you have to reverse 2nd position to 4th position in place.

    lst[2:5] = lst[2:5][::-1]
    

    Output:

    [1,2,5,4,3,6,7,8]

    0 讨论(0)
  • 2021-02-20 02:09

    I have two ways for in-place reversal, the simple way is to loop through the list half-way, swapping the elements with the respective mirror-elements. By mirror-element I mean (first, last), (2nd, 2nd-last), (3rd, 3rd-last), etc.

    def reverse_list(A):
        for i in range(len(A) // 2): # half-way
            A[i], A[len(A) - i - 1] = A[len(A) - i - 1], A[i] #swap
        return A
    

    The other way is similar to the above but using recursion as opposed to a "loop":

    def reverse_list(A):
        def rev(A, start, stop):
            A[start], A[stop] = A[stop], A[start] # swap
            if stop - start > 1: # until halfway
                rev(A, start + 1, stop - 1)
            return A
    
        return rev(A, 0, len(A) - 1)
    
    0 讨论(0)
  • 2021-02-20 02:15

    Easiest way to reverse a list in a partial or complete manner.

    listVar = ['a','b','c','d']
    def listReverse(list,start,end):
        while(start<end):
            temp = list[start]
            list[start] = list[end] #Swaping
            list[end]=temp
            start+=1
            end-=1
        print(list)
    
    
    listReverse(listVar,1,3)
    

    Output : - ['a', 'd', 'c', 'b']

    0 讨论(0)
提交回复
热议问题