当前位置: 首页 > 编程笔记 >

Python Deque双端队列操作方法

颛孙英才
2023-03-14
本文向大家介绍Python Deque双端队列操作方法,包括了Python Deque双端队列操作方法的使用技巧和注意事项,需要的朋友参考一下

在Python中,双端队列是一种数据结构,例如堆栈和队列。它允许从队列的两端进行追加和弹出操作。这使其与其余数据结构不同。下面列出了适用于双端队列的各种操作。在本文中,我们将看到每个操作的示例。该集合模块用于实现双端队列。

双端队列操作

以下是使用双端队列进行的一些有用操作

  • append() - 此函数用于将其参数中的值插入到双队列的右端。

  • appendleft() - 此函数用于将其参数中的值插入到双端队列的左端。

  • pop() - 此函数用于从双端队列的右端删除参数。

  • popleft()- 此函数用于从双端队列的左端删除一个参数。

  • extend(iterable) - 此函数用于在双端队列的右端添加多个值。传递的参数是可迭代的。

  • extendleft(iterable) - 此函数用于在双端队列的左端添加多个值。传递的参数是可迭代的。由于左侧追加,顺序颠倒了。

  • reverse() - 此函数用于反转双端队列元素的顺序。

  • rotation() - 该函数将双端队列旋转参数中指定的数字。如果指定的数字为负,则向左旋转。其他旋转向右。

示例

下面的程序显示了如何使用双端队列和collections模块实现上述操作。

import collections
de = collections.deque([10,20,30,40])
print(de)
de.append(50)
print ("\nAppending at right the deque is : ")
print (de)
de.appendleft(60)
print ("\nAppending at left the deque is : ")
print (de)
de.pop()
print ("\nDeleting from right the deque is: ")
print (de)
de.popleft()
print ("\nDeleting from left the deque is: ")
print (de)
de.extend([70,80])
print ("\nExtending deque at end is : ")
print (de)
de.extendleft([100,90])
print ("\nExtending deque at beginning is : ")
print (de)
de.rotate(-2)
print ("\nRotating deque is : ")
print (de)
de.reverse()
print ("\nReversing deque is : ")
print (de)

输出结果

运行上面的代码给我们以下结果-

deque([10, 20, 30, 40])
Appending at right the deque is :
deque([10, 20, 30, 40, 50])
Appending at left the deque is :
deque([60, 10, 20, 30, 40, 50])
Deleting from right the deque is:
deque([60, 10, 20, 30, 40])
Deleting from left the deque is:
deque([10, 20, 30, 40])
Extending deque at end is :
deque([10, 20, 30, 40, 70, 80])
Extending deque at beginning is :
deque([90, 100, 10, 20, 30, 40, 70, 80])
Rotating deque is :
deque([10, 20, 30, 40, 70, 80, 90, 100])
Reversing deque is :
deque([100, 90, 80, 70, 40, 30, 20, 10])
 类似资料:
  • queue 在java5中新增加了java.util.Queue接口,用以支持队列的常见操作。该接口扩展了java.util.Collection接口。除了基本的 Collection 操作外,队列还提供其他的插入、提取和检查操作。 每个方法都存在两种形式:一种抛出异常(操作失败时),另一种返回一个特殊值(null 或 false,具体取决于操作)。 抛出异常 返回特殊值 插入 add

  • 本文向大家介绍jQuery队列操作方法实例,包括了jQuery队列操作方法实例的使用技巧和注意事项,需要的朋友参考一下 jQuery核心中, 有一组队列控制方法, 这组方法由queue()/dequeue()/clearQueue()三个方法组成, 它对需要连续按序执行的函数的控制可以说是简明自如, 主要应用于animate ()方法, ajax以及其他要按时间顺序执行的事件中. 先解释一下这组方

  • 双端队列(Double-ended Queue) 注:队列是一种只允许在一端删除而在另一端插入的数据结构。双端队列(Deque)是队列的一种拓展,它可以在队列的两端进行插入和删除。 1. 双端队列(Deque)的概念 1.1 双端队列的定义 双端队列是限定插入和删除操作在表的两端进行的线性表,是一种具有队列和栈的性质的数据结构。 1.2 双端队列的特点 双端队列中的元素可以从两端进队和出队,其限定

  • blpop key1...keyN timeout 从左到右扫描返回对第一个非空list进行lpop操作并返回,比如blpop list1 list2 list3 0 ,如果list不存在list2,list3都是非空则对list2做lpop并返回从list2中删除的元素。如果所有的list都是空或不存在,则会阻塞timeout秒,timeout为0表示一直阻塞。当阻塞时,如果有client对ke

  • 主要内容:1 LinkedBlockingDeque的概述,2 LinkedBlockingDeque的原理,2.1 主要属性,2.2 构造器,2.3 入队操作,2.4 出队操作,2.5 检查操作,2.6 size操作,2.7 迭代操作,3 LinkedBlockingDeque的总结基于JDK1.8详细介绍了LinkedBlockingDeque的底层源码实现,包括双端队列的入队列、出队列、迭代等操作源码。实际上LinkedBlockingDeque的源码还是非常简单的! 1 LinkedBl

  • 本文向大家介绍JS中队列和双端队列实现及应用详解,包括了JS中队列和双端队列实现及应用详解的使用技巧和注意事项,需要的朋友参考一下 队列 队列 双端队列数据结构 应用 用击鼓传花游戏模拟循环队列 用双端对列检查一个词是否构成回文 生成 1 到 n 的二进制数 队列和双端队列 队列遵循先进后出(FIFO, 也称为先来先服务) 原则的. 日常有很多这样场景: 排队购票、银行排队等. 由对列的特性,银行