Efficient way to rotate a list in python

后端 未结 26 1180
一生所求
一生所求 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 03:45

    For a list X = ['a', 'b', 'c', 'd', 'e', 'f'] and a desired shift value of shift less than list length, we can define the function list_shift() as below

    def list_shift(my_list, shift):
        assert shift < len(my_list)
        return my_list[shift:] + my_list[:shift]
    

    Examples,

    list_shift(X,1) returns ['b', 'c', 'd', 'e', 'f', 'a'] list_shift(X,3) returns ['d', 'e', 'f', 'a', 'b', 'c']

    0 讨论(0)
  • 2020-11-22 03:47

    I think you've got the most efficient way

    def shift(l,n):
        n = n % len(l)  
        return l[-U:] + l[:-U]
    
    0 讨论(0)
  • 2020-11-22 03:47

    Following function copies sent list to a templist, so that pop function does not affect the original list:

    def shift(lst, n, toreverse=False):
        templist = []
        for i in lst: templist.append(i)
        if toreverse:
            for i in range(n):  templist = [templist.pop()]+templist
        else:
            for i in range(n):  templist = templist+[templist.pop(0)]
        return templist
    

    Testing:

    lst = [1,2,3,4,5]
    print("lst=", lst)
    print("shift by 1:", shift(lst,1))
    print("lst=", lst)
    print("shift by 7:", shift(lst,7))
    print("lst=", lst)
    print("shift by 1 reverse:", shift(lst,1, True))
    print("lst=", lst)
    print("shift by 7 reverse:", shift(lst,7, True))
    print("lst=", lst)
    

    Output:

    lst= [1, 2, 3, 4, 5]
    shift by 1: [2, 3, 4, 5, 1]
    lst= [1, 2, 3, 4, 5]
    shift by 7: [3, 4, 5, 1, 2]
    lst= [1, 2, 3, 4, 5]
    shift by 1 reverse: [5, 1, 2, 3, 4]
    lst= [1, 2, 3, 4, 5]
    shift by 7 reverse: [4, 5, 1, 2, 3]
    lst= [1, 2, 3, 4, 5]
    
    0 讨论(0)
  • 2020-11-22 03:47

    I was looking for in place solution to this problem. This solves the purpose in O(k).

    def solution(self, list, k):
        r=len(list)-1
        i = 0
        while i<k:
            temp = list[0]
            list[0:r] = list[1:r+1]
            list[r] = temp
            i+=1
        return list
    
    0 讨论(0)
  • 2020-11-22 03:49

    This also depends on if you want to shift the list in place (mutating it), or if you want the function to return a new list. Because, according to my tests, something like this is at least twenty times faster than your implementation that adds two lists:

    def shiftInPlace(l, n):
        n = n % len(l)
        head = l[:n]
        l[:n] = []
        l.extend(head)
        return l
    

    In fact, even adding a l = l[:] to the top of that to operate on a copy of the list passed in is still twice as fast.

    Various implementations with some timing at http://gist.github.com/288272

    0 讨论(0)
  • 2020-11-22 03:50

    It depends on what you want to have happen when you do this:

    >>> shift([1,2,3], 14)
    

    You might want to change your:

    def shift(seq, n):
        return seq[n:]+seq[:n]
    

    to:

    def shift(seq, n):
        n = n % len(seq)
        return seq[n:] + seq[:n]
    
    0 讨论(0)
提交回复
热议问题