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() 函数用于列表 number,其参数为 i,这会将列表中每个元素的索引位置移位 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 类提供的内置函数,用于执行旋转。尽管不太为人所知,但此函数非常有用。
算法
首先从 collection 模块导入 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]
结论
在本文中,我们简要解释了旋转列表中元素的四种不同方法。
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP