Python deque

队列是一个核心库,允许用户根据先进先出 ( 先进先出)原则定义列表。相比之下,Python 中的 Deque 拥有相反的原理: LIFO(后进先出)队列。在下面的教程中,我们将只通过一些例子来了解 Python 中的 Deque 是什么。

那么,让我们开始吧。

理解 Python 中的“得”字

一个德清,也称为一个双端队列,具有从任意一端插入和删除数据元素的属性。德格模块是图书馆的一部分,被称为收藏。它包含添加和移除数据元素的属性,这些数据元素可以通过参数直接调用。为了申报一个德格,我们必须先导入收藏库。

让我们考虑以下语法来理解 Python 中的 deque 模块是如何工作的。

语法:


# importing the deque module
# from the collections library
from collections import deque
# declaring the deque
list_name = deque()

说明:

在上面的代码片段中,我们已经从藏书库中导入了德格模块,并通过将列表的名称,即上面案例中的列表 _ 名称分配给德格()模块来声明德格。这里我们还可以观察到,为了实现这些内置方法,我们不需要任何类。它们可以直接实现。

让我们考虑一个基于de quee模块的简单例子。

示例:


# importing the deque module
# from the collections library
from collections import deque

# declaring the deque
fruit_list = deque(['Apple', 'Mango', 'Peaches', 'Banana', 'Papaya'])

# printing the deque
print(fruit_list)

输出:

deque(['Apple', 'Mango', 'Peaches', 'Banana', 'Papaya'])

说明:

在上面的例子中,我们已经从收藏库中导入了德格模块。然后,我们使用德格模块将水果列表定义为德格,指定了一些水果名称。然后,我们为用户打印了声明的德奎。结果,成功打印出包含一串水果名称的已声明的德格。

现在,让我们了解一下德客上的各种操作。

德格上的一些运算

有各种各样的操作可以在德格使用。下面列出了其中一些及其描述:

| 南号码 | 操作 | 描述 |
| one | 追加() | append()函数用于将参数中的数据元素添加到 deque 的右端。 |
| Two | appendleft() | appendleft()函数用于将参数中的数据元素添加到 deque 的左端。 |
| three | 流行音乐() | pop()函数用于从 deque 的右端删除数据元素。 |
| four | popleft() | popleft()函数用于从 deque 的右端删除数据元素。 |
| five | 索引(元素、开始、结束) | index()函数用于返回参数中指定的第一个索引值,从开始到结束索引开始搜索。 |
| six | 插入(I,x) | insert()函数用于在参数中提到的索引号“I”处插入参数“x”中描述的值。 |
| seven | 移除() | remove()函数用于删除参数中指定值的第一次出现。 |
| eight | 计数() | count()函数用于计算参数中指定值的总出现次数。 |
| nine | 扩展(可迭代) | extend()函数用于在 deque 的右端插入多个数据元素。传递的参数是可迭代的。 |
| Ten | extendleft(可迭代) | extendleft()函数用于在 deque 的左端插入多个数据元素。传递的参数是可迭代的。作为左追加的输出,顺序也是相反的。 |
| Eleven | 反向() | reverse()函数用于反转数据元素的顺序。 |
| Twelve | 旋转() | rotate()函数用于将 deque 旋转参数中提到的数值。如果提到的数字是负值,那么旋转发生在左边。否则旋转向右。 |

现在让我们考虑一些基于de quee模块的例子。

示例:


# importing the collections library
# for deque operations
import collections

# declaring the deque
my_deque = collections.deque([10, 20, 30, 40, 50])

# using the append() function to add 
# data element at right end
# inserting 60 at the end of the deque
my_deque.append(60)

# printing the resultant deque
print( "The deque after appending at right: " )
print( my_deque )

# using the appendleft() function to add
# data element at left end
# inserting 70 at the starting of the deque
my_deque.appendleft(70)

# printing the resultant deque
print( "The deque after appending at left: " )
print( my_deque )

# using the pop() function to remove
# data element from the right end
# removing 60 from the right end of deque
my_deque.pop()

# printing the resultant deque
print( "The deque after removing from right: " )
print( my_deque )

# using the popleft() function to remove
# data element from the left end
# removing 70 from the left end of deque
my_deque.popleft()

# printing the resultant deque
print("The deque after removing from left: " )
print( my_deque )

输出:

The deque after appending at right: 
deque([10, 20, 30, 40, 50, 60])
The deque after appending at left:
deque([70, 10, 20, 30, 40, 50, 60])
The deque after removing from right:
deque([70, 10, 20, 30, 40, 50])
The deque after removing from left:
deque([10, 20, 30, 40, 50])

说明:

在上面的代码片段中,我们已经导入了集合库,并声明了一个 deque。然后我们使用了像 append() 和 appendleft() 这样的操作,以便将一些数据元素插入到 deque 的两端,并为用户打印修改后的 deque。类似地,我们使用了 pop() 和 popleft() 等操作,以便从 deque 的两端移除数据元素,并为用户打印结果 deque。

示例:


# importing the collections library
import collections

# declaring the deque
my_deque = collections.deque(['Jan', 'Feb', 'Mar', 'Mar', 'Feb', 'April', 'Feb'])

# using the index() function to print
# the first occurrence of data element: Feb
print( "The first occurs of 'Feb' at a position: " )
print( my_deque.index('Feb', 2, 7) )

# using the insert() function to insert
# the data element 'Jan' at 4th position
my_deque.insert(3,'Jan')

# printing the resultant deque
print( "The deque after inserting 'Jan' at 4th position: " )
print( my_deque )

# using the count() function to count
# the occurrences of data element 'Feb'
print( "The count of 'Feb' in deque: " )
print( my_deque.count('Feb') )

# using the remove() function to remove
# the first occurrence of data element 'Mar'
my_deque.remove('Mar')

# printing the resultant deque
print( "The deque after removing the first occurrence of 'Mar': " )
print( my_deque )

输出:

The first occurs of 'Feb' at a position:
4
The deque after inserting 'Jan' at 4th position:
deque(['Jan', 'Feb', 'Mar', 'Jan', 'Mar', 'Feb', 'April', 'Feb'])
The count of 'Feb' in deque:
3
The deque after removing the first occurrence of 'Mar':
deque(['Jan', 'Feb', 'Jan', 'Mar', 'Feb', 'April', 'Feb'])

说明:

在上面的代码片段中,我们再次导入了集合库,并声明了一个 deque。然后,我们使用了索引()操作来分别检查索引号 2 和 7 之间的数据元素【2 月】的首次出现。然后我们使用了 insert() 操作,以便在第 4 个位置插入数据元素‘Jan’。然后,我们使用 count() 操作来计算数据元素“2 月”在 deque 中的出现次数。最后,我们使用了 remove() 操作,以便将数据元素‘Mar’的第一次出现从 deque 中删除,并为用户打印结果 deque。


原创文章,作者:L42C2,如若转载,请注明出处:https://www.506064.com/n/127695.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
L42C2L42C2
上一篇 2024-10-03 23:16
下一篇 2024-10-03 23:16

相关推荐

  • Python中引入上一级目录中函数

    Python中经常需要调用其他文件夹中的模块或函数,其中一个常见的操作是引入上一级目录中的函数。在此,我们将从多个角度详细解释如何在Python中引入上一级目录的函数。 一、加入环…

    编程 2025-04-29
  • Python周杰伦代码用法介绍

    本文将从多个方面对Python周杰伦代码进行详细的阐述。 一、代码介绍 from urllib.request import urlopen from bs4 import Bea…

    编程 2025-04-29
  • Python计算阳历日期对应周几

    本文介绍如何通过Python计算任意阳历日期对应周几。 一、获取日期 获取日期可以通过Python内置的模块datetime实现,示例代码如下: from datetime imp…

    编程 2025-04-29
  • 如何查看Anaconda中Python路径

    对Anaconda中Python路径即conda环境的查看进行详细的阐述。 一、使用命令行查看 1、在Windows系统中,可以使用命令提示符(cmd)或者Anaconda Pro…

    编程 2025-04-29
  • Python列表中负数的个数

    Python列表是一个有序的集合,可以存储多个不同类型的元素。而负数是指小于0的整数。在Python列表中,我们想要找到负数的个数,可以通过以下几个方面进行实现。 一、使用循环遍历…

    编程 2025-04-29
  • python强行终止程序快捷键

    本文将从多个方面对python强行终止程序快捷键进行详细阐述,并提供相应代码示例。 一、Ctrl+C快捷键 Ctrl+C快捷键是在终端中经常用来强行终止运行的程序。当你在终端中运行…

    编程 2025-04-29
  • Python字典去重复工具

    使用Python语言编写字典去重复工具,可帮助用户快速去重复。 一、字典去重复工具的需求 在使用Python编写程序时,我们经常需要处理数据文件,其中包含了大量的重复数据。为了方便…

    编程 2025-04-29
  • 蝴蝶优化算法Python版

    蝴蝶优化算法是一种基于仿生学的优化算法,模仿自然界中的蝴蝶进行搜索。它可以应用于多个领域的优化问题,包括数学优化、工程问题、机器学习等。本文将从多个方面对蝴蝶优化算法Python版…

    编程 2025-04-29
  • Python程序需要编译才能执行

    Python 被广泛应用于数据分析、人工智能、科学计算等领域,它的灵活性和简单易学的性质使得越来越多的人喜欢使用 Python 进行编程。然而,在 Python 中程序执行的方式不…

    编程 2025-04-29
  • Python清华镜像下载

    Python清华镜像是一个高质量的Python开发资源镜像站,提供了Python及其相关的开发工具、框架和文档的下载服务。本文将从以下几个方面对Python清华镜像下载进行详细的阐…

    编程 2025-04-29

发表回复

登录后才能评论