【问题标题】:Rotating a list without using collection.deque在不使用 collection.deque 的情况下旋转列表
【发布时间】:2015-12-03 07:48:36
【问题描述】:

我想做一个脚本。程序应该得到一些列表L,其中包含值和自然数N
如果N>0,则列表的对象将N 向左移动。
如果N<0,则列表的对象将abs(N) 向右移动。
如果N=0 列表保持不变...

例如:对于N=1L=[1,2,3,4,5],输出为[2,3,4,5,1]
对于相同的列表和N=-1,输出为[5,1,2,3,4]

我实际上是使用collection.deque 完成的,但我只想使用列表、for 循环和“if”来完成此操作。

我很难理解如何让物体移动。

l = input("enter list:")
N = input("enter number of rotations:")
import collections
d = collections.deque(l)
d.rotate(-N)
print d

【问题讨论】:

    标签: python list python-2.7


    【解决方案1】:

    您可以使用列表切片:

    def rotate(L, N):
        if not L or N % len(L) == 0:
            return L
        return L[N % len(L):] + L[:N % len(L)]
    
    L = [1, 2, 3, 4, 5]
    
    for N in range(-3, 4):
        print(rotate(L, N))
    

    输出:

    [3, 4, 5, 1, 2]
    [4, 5, 1, 2, 3]
    [5, 1, 2, 3, 4]
    [1, 2, 3, 4, 5]
    [2, 3, 4, 5, 1]
    [3, 4, 5, 1, 2]
    [4, 5, 1, 2, 3]
    

    请注意,如果您使用list,则旋转的time complexity 通常与列表中的元素数量呈线性关系,因为您必须移动所有现有元素。另一方面,deque.rotate()O(k)k 是步数。因此,如果您需要多次旋转deque.rotate() 是要走的路。

    【讨论】:

      【解决方案2】:

      这行得通:

      result = l[N % len(l):]+l[:N % len(l)]
      

      【讨论】:

      • 不考虑空列表。
      • 是的,你是对的。可以通过使用 result = l[N % len(l):]+l[:N % len(l)] if l else [] 来做到这一点
      【解决方案3】:

      如果您可以选择使用 numpy,有一个名为 roll 的方法,您可能想看看:

      import numpy as np
      
      array = np.arange(1, 6)  # 1 to 5
      print array
      print np.roll(array, 0)
      print np.roll(array, 2)
      print np.roll(array, -2)
      print np.roll(array, 17)
      

      结果如预期:

      [1 2 3 4 5]
      [1 2 3 4 5]
      [4 5 1 2 3]
      [3 4 5 1 2]
      [4 5 1 2 3]
      

      【讨论】:

        【解决方案4】:

        如果您追求简单的列表,这里介绍如何在 Python 3 中实现(基于您的 P2 示例)。

        L = list(map(int, input("Enter numbers: ").split(" ")))
        N = int(input("Enter how many rotations to perform: "))
        print(L[N:] + L[:N])
        

        【讨论】:

          【解决方案5】:

          如果您不想创建新列表,可以尝试就地反向,

          def reverse(nums, start, end):
              i = start
              j = end - 1
              while i < j:
                  tmp = nums[i]
                  nums[i] = nums[j]
                  nums[j] = tmp
                  i += 1
                  j -= 1
          
          def rotate(nums, n):
              if n == 0:
                  return nums
              length = len(nums)
              if n < 0:
                  n = length + n
              reverse(nums, 0, n)
              reverse(nums, n, length)
              reverse(nums, 0, length)
              return nums
          
          >>> rotate([1, 2, 3, 4, 5], 1)
          [2, 3, 4, 5, 1]
          >>> rotate([1, 2, 3, 4, 5], -1)
          [5, 1, 2, 3, 4]
          

          【讨论】:

            【解决方案6】:

            谢谢大家! :) 你的回答很好,对我很有用。

            这是我对这个问题的解决方案(鉴于我为生物学家的基本 Python 课程编写了这些脚本):

            L = input('Enter list of numbers, please: ')
            N = input('Enter how many places to rotate: ')
            L1 = L[N:]
            L2 = L[:N]
            L = L1 + L2
            print L
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2017-06-11
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2019-08-24
              • 1970-01-01
              相关资源
              最近更新 更多