Python 用于旋转列表元素

Python 用于旋转列表元素

在Python中,可以使用列表将多个项保存在一个变量中。列表是Python的四种内置数据类型之一,用于存储数据集合。其他三种类型,元组,集合和字典,分别用于不同的功能。列表使用方括号构建。因为列表不需要是同质的,所以它们是Python中最有用的工具。一个列表包含像字符串、对象和整数之类的数据类型。列表在生成后可以被修改,因为它们是可变的。

本文的重点是简写和一行代码或一个词来表示这个操作的多种快速方法。对于程序员来说,这个操作非常重要,可以完成许多工作。我们将使用Python来完成这个任务的四种不同方法。

使用列表推导式

在使用这种方法时,我们只需要在旋转到特定位置后重新分配索引给列表中的每个元素。由于其实现较小,这个方法在完成任务中起着重要作用。

步骤

  • 首先定义一个列表。

  • 使用列表推导式。

  • 为了应用两个不同的方向,右侧(i-index)和左侧(i+index)。

  • 打印输出列表。

语法

左旋

list_1 = [list_1[(i + 3) % len(list_1)]

右旋

list_1 = [list_1[(i - 3) % len(list_1)]

示例

在这个代码中,我们使用了列表推导式来实现列表元素的右旋和左旋。使用for循环来遍历列表元素。

list_1 = [10, 14, 26, 37, 42]
print (" Primary list : " + str(list_1))
list_1 = [list_1[(i + 3) % len(list_1)]
   for i, x in enumerate(list_1)]
print ("Output of the list after left rotate by 3 : " + str(list_1))
list_1 = [list_1[(i - 3) % len(list_1)]
   for i, x in enumerate(list_1)]
print ("Output of the list after right rotate by 3(back to primary list) : "+str(list_1))
list_1 = [list_1[(i + 2) % len(list_1)]
   for i, x in enumerate(list_1)]
print ("Output of the list after left rotate by 2 : " + str(list_1))
list_1 = [list_1[(i - 2) % len(list_1)]
   for i, x in enumerate(list_1)]
print ("Output of the list after right rotate by 2 : "+ str(list_1))

输出

Primary list : [10, 14, 26, 37, 42]
Output of the list after left rotate by 3 : [37, 42, 10, 14, 26]
Output of the list after right rotate by 3(back to primary list) : [10, 14, 26, 37, 42]
Output of the list after left rotate by 2 : [26, 37, 42, 10, 14]
Output of the list after right rotate by 2 : [10, 14, 26, 37, 42]

在这段代码中,我们使用列表推导式来对列表中的元素进行右旋和左旋。使用for循环遍历元素列表。

使用切片

这种特定的技术是标准的技术。通过旋转数字,它简单地将后面切片的部分与前面切片的部分连接起来。

步骤

  • 首先定义一个列表。

  • 使用切片方法。

  • 打印右旋和左旋后的每个列表。

语法

用于切片

左旋 –

list_1 = list_1[3:] + list_1[:3]

右旋 −

list_1 = list_1[-3:] + list_1[:-3]

示例

以下程序重新排列一个列表的元素。原始列表是[11, 34, 26, 57, 92]。首先将列表左移3个单位。也就是将前三个元素移动到末尾,得到[57, 92, 11, 34, 26]。然后再右移3个单位,使得最后三个元素回到原始位置,得到[11, 34, 26, 57, 92]。

接着再右移2个单位,使得最后两个元素向前移动,得到[26, 57, 92, 11, 34]。最后再左移1个单位,将第一个元素移动到末尾,得到[57, 92, 11, 34, 26]。

list_1 = [11, 34, 26, 57, 92]
print (" Primary list : " + str(list_1))
list_1 = list_1[3:] + list_1[:3]
print ("Output of the list after left rotate by 3 : " + str(list_1))
list_1 = list_1[-3:] + list_1[:-3]
print ("Output of the list after right rotate by 3(back to Primary list) : "+str(list_1))
list_1 = list_1[-2:] + list_1[:-2]
print ("Output of the list after right rotate by 2 : "+ str(list_1))
list_1 = list_1[1:] + list_1[:1]
print ("Output of the list after left rotate by 1 : " + str(list_1))

输出

Primary list : [11, 34, 26, 57, 92]
Output of the list after left rotate by 3 : [57, 92, 11, 34, 26]
Output of the list after right rotate by 3(back to Primary list) : [11, 34, 26, 57, 92]
Output of the list after right rotate by 2 : [57, 92, 11, 34, 26]
Output of the list after left rotate by 1 : [92, 11, 34, 26, 57]

使用NumPy模块

使用给定的轴,我们还可以使用Python的NumPy.roll模块来旋转列表中的元素。由于这个操作,输入数组的元素会被移动。如果一个元素从首位移动到末位,它会返回到初始位置。

步骤

  • 导入NumPy.roll模块

  • 定义列表并给出特定的索引

  • 输出列表

示例

创建一个名为”number”的列表,并赋值为1、2、4、10、18和83。将变量i设置为1。然后使用NumPy库的np.roll()函数,该函数的参数为i,将列表number中的每个元素向后移动1个索引位置(第一个元素变成最后一个元素)。

import numpy as np
if __name__ == '__main__':
   number = [1, 2, 4, 10, 18, 83]
   i = 1
   x = np.roll(number, i)
   print(x)

输出

[83 1 2 4 10 18]

使用collections.deque.rotate()

rotate()函数是由collections模块中的deque类提供的一个内建函数,用于进行循环移位操作。尽管不太被人们熟知,但这个函数非常有用。

步骤

  • 首先从collections模块导入deque类。

  • 定义一个列表。

  • 打印原始列表。

  • 使用rotate()函数进行元素的循环移位。

  • 打印输出结果。

示例

以下程序使用了collections模块中的deque数据结构来对列表进行循环移位操作。首先打印原始列表,然后将其左移3位并打印出新的循环移位后的列表。接着将其右移3位(恢复到原始位置)并打印出结果列表。

from collections import deque
list_1 = [31, 84, 76, 97, 82]
print ("Primary list : " + str(list_1))
list_1 = deque(list_1)
list_1.rotate(-3)
list_1 = list(list_1)
print ("Output list after left rotate by 3 : " + str(list_1))
list_1 = deque(list_1)
list_1.rotate(3)
list_1 = list(list_1)
print ("Output list after right rotate by 3(back to primary list) : "+ str(list_1))

输出

Primary list : [31, 84, 76, 97, 82]
Output list after left rotate by 3 : [97, 82, 31, 84, 76]
Output list after right rotate by 3(back to primary list) : [31, 84, 76, 97, 82]

结论

在本文中,我们简单介绍了四种不同的列表元素旋转方法。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程