队列(Queue)是一个核心库,允许用户根据FIFO(先进先出)原则定义一个列表。相反,Python中的Deque具有相反的原则:LIFO(后进先出)队列。在以下教程中,我们只会了解Python中的Deque是什么,附有一些示例。

所以,让我们开始吧。

理解Python中的Deque

Deque,也被称为双端队列,具有从两端插入和删除数据元素的属性。deque模块是一个名为collections的库的一部分。它包含了可以直接使用参数调用的添加和删除数据元素的属性。为了声明一个deque,我们首先需要导入collections库。

让我们考虑以下语法以了解Python中deque模块的工作方式。

语法:

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

解释:

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

让我们看一个基于deque模块的简单示例。

示例:

# 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'])

解释:

在上面的示例中,我们从collections库中导入了deque模块。然后,我们使用deque模块定义了一个水果列表,指定了一些水果名称。然后,我们为用户打印了声明的Deque。结果,包含一堆水果名称的声明的Deque已成功打印。

现在,让我们了解Deque上的各种操作。

Deque的一些操作

可以在Deque中使用各种操作。以下是其中一些操作及其描述:

序号操作描述
1append()append()函数用于将其参数中的数据元素添加到deque的右端。
2appendleft()appendleft()函数用于将其参数中的数据元素添加到deque的左端。
3pop()pop()函数用于从deque的右端删除数据元素。
4popleft()popleft()函数用于从deque的左端删除数据元素。
5index(element, begin, end)index()函数用于返回在参数中指定的第一个索引值,从begin到end索引开始搜索。
6insert(i, x)insert()函数用于在参数中指定的索引号'i'处插入参数'x'中描述的值。
7remove()remove()函数用于删除参数中指定的值的第一次出现。
8count()count()函数用于计算参数中指定的值的总出现次数。
9extend(iterable)extend()函数用于在deque的右端插入多个数据元素。传递的参数是可迭代的。
10extendleft(iterable)extendleft()函数用于在deque的左端插入多个数据元素。传递的参数是可迭代的。输出的顺序也会颠倒为左侧附加。
11reverse()reverse()函数用于反转deque数据元素的顺序。
12rotate()rotate()函数用于按照参数中指定的数字旋转deque。如果指定的数字是负数,旋转将发生在左侧。否则,旋转将发生在右侧。

现在让我们考虑一些基于deque模块的示例。

示例:

# 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])

解释:

在上面的代码片段中,我们导入了collections库并声明了一个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'])

解释:

在上面的代码片段中,我们再次导入了collections库并声明了一个deque。然后,我们使用index()操作来查看数据元素'Feb'在索引号27之间的第一次出现。然后,我们使用insert()操作在第4个位置插入数据元素'Jan'。我们还使用count()操作来计算deque中数据元素'Feb'的出现次数。最后,我们使用remove()操作来从deque中删除数据元素'Mar'的第一次出现,并为用户打印了结果deque。

标签: Tkinter教程, Tkinter安装, Tkinter库, Tkinter入门, Tkinter学习, Tkinter入门教程, Tkinter, Tkinter进阶, Tkinter指南, Tkinter学习指南, Tkinter进阶教程, Tkinter编程