Python程序旋转列表元素

WBOY
WBOY 转载
2023-08-19 10:01:09 833浏览

Python程序旋转列表元素

在Python中,可以使用列表来在单个变量中维护多个项目。列表是Python的四种内置数据类型之一,用于存储数据集合。另外三种类型,元组、集合和字典,各自具有不同的功能。列表使用方括号进行构造。由于列表不必是同质的,它们是Python中最有用的工具。一个列表可以包含字符串、对象和整数等数据类型。列表可以在生成后进行修改,因为它们是可变的。

本文的重点是速记和用一句话或一个词来表达这个概念的许多快捷方式。这个操作对于程序员来说非常重要,可以完成许多工作。我们将使用Python来展示四种不同的方法来完成这个任务。

Using The List Comprehension

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

Algorithm

  • Defining a list first.

  • Use the list comprehension.

  • For applying two different sides right(i-index) and left(i+index).

  • Print the output list.

语法

#左旋转

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

#For right rotate

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

Example

Here, in this code we have used the list comprehension to rotate the elements in a list that is the right and left rotate. For loop is used to iterate through the list of elements.

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]

Here, in this code we have used the list comprehension to rotate the elements in a list that is the right and left rotate. For loop is used to iterate through the list of elements.

使用切片

This specific technique is the standard technique. With the rotation number, it simply joins the later-sliced component to the earlier-sliced part.

Algorithm

  • Defining a list first.

  • Use slicing method.

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

语法

FOR SLICING

#左旋转 -

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

#右旋转 -

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

Example

The following program rearranges the elements of a list. The original list is [11, 34, 26, 57, 92]. First rotate 3 units to the left. That is, the first three elements are moved to the end, resulting in [57, 92, 11, 34, 26]. Then rotate right by 3 so the last three elements move back and forth to their original positions [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]

Using The Numpy Module

使用给定的轴,我们还可以使用python中的numpy.roll模块来旋转列表中的元素。输入数组的元素会因此被移动。如果一个元素从第一个位置移动到最后一个位置,它会回到初始位置。

Algorithm

  • 导入numpy.roll模块

  • Define the list and give the particular index.

  • Print the output list.

Example

A list 'number' is created and assigned the values 1, 2, 4, 10, 18 and 83. The variable i is set to 1. The np.roll() function from the NumPy library is then used on the list number with an argument of i which shifts each element in the list by 1 index position (the first element becomes last).

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]

Usingcollections.deque.rotate()

rotate()函数是由collections模块中的deque类提供的内置函数,用于实现旋转操作。尽管不太为人所知,但这个函数更加实用。

Algorithm

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

  • 定义一个列表

  • 打印主要列表

  • 使用rotate()函数旋转元素

  • Print the output.

Example

The following program uses the deque data structure from the collections module to rotate a list. The original list is printed, then it rotates left by 3 and prints out the new rotated list. It then rotates right (back to its original position) by 3 and prints out the resulting list.

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]

结论

在本文中,我们简要解释了四种不同的方法来旋转列表中的元素。

以上就是Python程序旋转列表元素的详细内容,更多请关注php中文网其它相关文章!

声明:本文转载于:tutorialspoint,如有侵犯,请联系admin@php.cn删除