queue python
时间: 2023-10-13 07:19:34 浏览: 101
A queue in Python is a data structure that allows elements to be added and removed in a "first in, first out" (FIFO) order. It is similar to a line of people waiting for service, where the first person in line gets served first.
In Python, a queue can be implemented using the built-in deque (double-ended queue) module. The deque module provides a fast and efficient way to add and remove elements from both ends of the queue.
Here's an example of how to create a queue using deque:
```
from collections import deque
queue = deque()
# Add elements to the queue
queue.append(1)
queue.append(2)
queue.append(3)
# Remove the first element from the queue
queue.popleft()
```
In the example above, we first import the deque module from the collections module. We then create an empty queue using the deque() constructor. We add elements to the queue using the append() method, and remove the first element using the popleft() method.
阅读全文