Efficient way to rotate a list in python

后端 未结 26 1182
一生所求
一生所求 2020-11-22 03:14

What is the most efficient way to rotate a list in python? Right now I have something like this:

>>> def rotate(l, n):
...     return l[n:] + l[:n]         


        
相关标签:
26条回答
  • 2020-11-22 04:00

    The following method is O(n) in place with constant auxiliary memory:

    def rotate(arr, shift):
      pivot = shift % len(arr)
      dst = 0
      src = pivot
      while (dst != src):
        arr[dst], arr[src] = arr[src], arr[dst]
        dst += 1
        src += 1
        if src == len(arr):
          src = pivot
        elif dst == pivot:
          pivot = src
    

    Note that in python, this approach is horribly inefficient compared to others as it can't take advantage of native implementations of any of the pieces.

    0 讨论(0)
  • 2020-11-22 04:00

    Jon Bentley in Programming Pearls (Column 2) describes an elegant and efficient algorithm for rotating an n-element vector x left by i positions:

    Let's view the problem as transforming the array ab into the array ba, but let's also assume that we have a function that reverses the elements in a specified portion of the array. Starting with ab, we reverse a to get arb, reverse b to get arbr, and then reverse the whole thing to get (arbr)r, which is exactly ba. This results in the following code for rotation:

    reverse(0, i-1)
    reverse(i, n-1)
    reverse(0, n-1)
    

    This can be translated to Python as follows:

    def rotate(x, i):
        i %= len(x)
        x[:i] = reversed(x[:i])
        x[i:] = reversed(x[i:])
        x[:] = reversed(x)
        return x
    

    Demo:

    >>> def rotate(x, i):
    ...     i %= len(x)
    ...     x[:i] = reversed(x[:i])
    ...     x[i:] = reversed(x[i:])
    ...     x[:] = reversed(x)
    ...     return x
    ... 
    >>> rotate(list('abcdefgh'), 1)
    ['b', 'c', 'd', 'e', 'f', 'g', 'h', 'a']
    >>> rotate(list('abcdefgh'), 3)
    ['d', 'e', 'f', 'g', 'h', 'a', 'b', 'c']
    >>> rotate(list('abcdefgh'), 8)
    ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
    >>> rotate(list('abcdefgh'), 9)
    ['b', 'c', 'd', 'e', 'f', 'g', 'h', 'a']
    
    0 讨论(0)
提交回复
热议问题