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中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

如何使用Python的count()函数计算列表中某个元素的数量,需要具体代码示例Python作为一种强大且易学的编程语言,提供了许多内置函数来处理不同的数据结构。其中之一就是count()函数,它可以用来计算列表中某个元素的数量。在本文中,我们将详细介绍如何使用count()函数,并提供具体的代码示例。count()函数是Python的内置函数,用于计算某

如何在iOS17中的iPhone上制作GroceryList在“提醒事项”应用中创建GroceryList非常简单。你只需添加一个列表,然后用你的项目填充它。该应用程序会自动将您的商品分类,您甚至可以与您的伴侣或扁平伙伴合作,列出您需要从商店购买的东西。以下是执行此操作的完整步骤:步骤1:打开iCloud提醒事项听起来很奇怪,苹果表示您需要启用来自iCloud的提醒才能在iOS17上创建GroceryList。以下是它的步骤:前往iPhone上的“设置”应用,然后点击[您的姓名]。接下来,选择i

在iOS17中,Apple在提醒应用程序中添加了一个方便的小列表功能,以便在您外出购买杂货时为您提供帮助。继续阅读以了解如何使用它并缩短您的商店之旅。当您使用新的“杂货”列表类型(在美国以外名为“购物”)创建列表时,您可以输入各种食品和杂物,并按类别自动组织它们。该组织使您在杂货店或外出购物时更容易找到您需要的东西。提醒中可用的类别类型包括农产品、面包和谷物、冷冻食品、零食和糖果、肉类、乳制品、鸡蛋和奶酪、烘焙食品、烘焙食品、家居用品、个人护理和健康以及葡萄酒、啤酒和烈酒。以下是在iOS17中创

我们在使用Word办公软件进行文档处理的时候,经常需要在文档里插入一些图片之类的素材,但是,为了排版美观的需要,我们还需要将图片进行一些特殊的排版,其中旋转处理是最基本的排版处理,但是,对于一些刚刚接触Word办公软件的职场新人来讲,可能还不太会在Word文档里处理图片。下边,我们就分享一下Word图片怎么旋转的方法,希望对你有所帮助和启发。1、首先,我们打开一个Word文档,随后,我们菜单栏点击插入-图片按钮,电脑中随意找一张图片插入,便于我们操作演示使用。2、如果我们要将图片进行旋转,接着需

如何使用Vue实现3D立体旋转特效作为一种流行的前端框架,Vue.js在开发动态网页和应用程序中起着重要的作用。它提供了一种直观、高效的方式来构建交互式界面,并且易于集成和扩展。本文将介绍如何使用Vue.js实现一个令人惊叹的3D立体旋转特效,并提供具体的代码示例。在开始之前,请确保您已经安装了Vue.js,并且对Vue.js的基本用法有一定的了解。如果您还

为了方便很多人移动办公,很多笔记本都带有无线网的功能,但有些人的电脑上无法显示WiFi列表,现在就给大家带来win7系统下遇到这种问题的处理方法,一起来看一下吧。win7无线网络列表显示不出来1、右键你电脑右下角的网络图标,选择“打开网络和共享中心”,打开后再点击左边的“更改适配器设置”2、打开后鼠标右键选择无线网络适配器,选择“诊断”3、等待诊断,如果系统诊断出问题那就修复它。4、修复完成之后,就可以看到WiFi列表了。

使用Python的reverse()函数反转列表,需要具体代码示例在Python中,我们经常需要在编程中对列表进行操作,其中反转列表是常见的一种需求,这时候我们可以使用Python内置的reverse()函数来实现。reverse()函数的作用是反转列表中的元素顺序,即将列表中第一个元素变为最后一个元素,第二个元素变为倒数第二个元素,以此类推。下面是使用Py

CSS实现加载动画效果的技巧和方法随着互联网的发展,加载速度成为了用户体验的重要指标之一。为了提升页面加载时的用户体验,我们通常会使用加载动画效果来增加页面的互动性和吸引力。而CSS作为前端开发中的重要技术之一,提供了许多实现加载动画效果的技巧和方法。本文将介绍几种常见的CSS加载动画效果的实现技巧和方法,并提供相应的代码示例。旋转加载动画旋转加载动画是一种
