Python – Queue.LIFOQueue 与 Collections.Deque
LIFOQueue和Deque都可以使用Python中的内置模块Queue和Collections使用,它们都是数据结构并且被广泛使用,但用途不同。在本文中,我们将考虑Queue.LIFOQueue和Collections.Deque在Python中的可用性、执行时间、工作、实现等方面的区别。
queue.LifoQueue:模块队列提供了一个 LIFO 队列,它在技术上用作堆栈。它通常用于同一进程中不同线程之间的通信。
下面是一个描述Lifo.Queue实现的程序:
Python3
# Import required module
import queue
# Initialize LIFO Queue
LIFOq = queue.LifoQueue(maxsize=3)
# qsize() give the maxsize of
# the Queue
print(LIFOq.qsize())
# Data Inserted as 5->9->1->7,
# same as Queue
LIFOq.put(1)
LIFOq.put(2)
LIFOq.put(3)
# Displaying if the Queue is full
print("Full: ", LIFOq.full())
# Displaying size of queue
print("Size: ", LIFOq.qsize())
# Data will be accessed in the
# reverse order Reverse of that
# of Queue
print(LIFOq.get())
print(LIFOq.get())
print(LIFOq.get())
# Displaying if the queue is empty or not
print("Empty: ", LIFOq.empty())
Python3
# Import required modules
import collections
# Initialize deque
Deque = collections.deque([10, 20, 30])
# Using append() to insert element at right end
# Inserts 0 at the end of deque
Deque.append(0)
# Printing modified deque
print("The deque after appending at right is:", Deque)
# Using appendleft() to insert element at left end
# Inserts 100 at the beginning of deque
Deque.appendleft(100)
# Printing modified deque
print("The deque after appending at left is: ", Deque)
# Using pop() to delete element from right end
# Deletes 0 from the right end of deque
Deque.pop()
# Printing modified deque
print("The deque after deleting from right is:", Deque);
# Using popleft() to delete element from left end
# Deletes 100 from the left end of deque
Deque.popleft()
# Printing modified deque
print("Queue:", Deque)
输出:
0
Full: True
Size: 3
3
2
1
Empty: True
collections.deque: Python中的 Deque(双端队列)是使用模块collections实现的。该数据结构主要用于队列。 FIFO 队列机制由append()和popleft()实现。与列表相比,它的操作要快得多。
下面是一个程序,说明了collections.deque的实现:
蟒蛇3
# Import required modules
import collections
# Initialize deque
Deque = collections.deque([10, 20, 30])
# Using append() to insert element at right end
# Inserts 0 at the end of deque
Deque.append(0)
# Printing modified deque
print("The deque after appending at right is:", Deque)
# Using appendleft() to insert element at left end
# Inserts 100 at the beginning of deque
Deque.appendleft(100)
# Printing modified deque
print("The deque after appending at left is: ", Deque)
# Using pop() to delete element from right end
# Deletes 0 from the right end of deque
Deque.pop()
# Printing modified deque
print("The deque after deleting from right is:", Deque);
# Using popleft() to delete element from left end
# Deletes 100 from the left end of deque
Deque.popleft()
# Printing modified deque
print("Queue:", Deque)
输出:
The deque after appending at right is: deque([10, 20, 30, 0])
The deque after appending at left is: deque([100, 10, 20, 30, 0])
The deque after deleting from right is: deque([100, 10, 20, 30])
Queue: deque([10, 20, 30])
LIFOQueue 和 Deque 的区别:
Sr. no. | LIFO Queue | Dequeue |
---|---|---|
1 | It implements stacks | It implements a double-edged queue |
2 | Present in Queue module | Present in Collections module |
3 | Allows various threads to communicate using queued data or messages | Simply intended to be a data structure |
4 | Fewer features (operations and methods) | Large Variety of features (operations and methods) |
5 | Follows Last In First Out | Follows First In First Out |
6 | Slow operations (long execution time) | High operations(very low execution time) |
6 | Not commonly used, usually used for thread communication operations. | Mostly used for data structure operations. |