Hàng đợi ưu tiên là một phần mở rộng của hàng đợi với các thuộc tính sau.
- Một yếu tố có mức độ ưu tiên cao được khử trùng trước một phần tử có mức độ ưu tiên thấp.
- Nếu hai yếu tố có cùng mức độ ưu tiên, chúng được phục vụ theo đơn đặt hàng của chúng trong hàng đợi.
queue.PriorityQueue[maxsize]
Nó là một nhà xây dựng cho một hàng đợi ưu tiên. Tối đa là số lượng các phần tử có thể được chèn vào hàng đợi, giá trị mặc định của nó là 0. Nếu giá trị tối đa nhỏ hơn hoặc bằng 0, thì kích thước hàng đợi là vô hạn. Các mục được truy xuất thứ tự ưu tiên [thấp nhất đầu tiên].
Functions-
- đặt [] - đặt một mục vào hàng đợi. Puts an item into the queue.
- Nhận [] - Xóa và trả về một mục từ hàng đợi. Removes and returns an item from the queue.
- Qsize [] - Trả về kích thước hàng đợi hiện tại. Returns the current queue size.
- trống [] - trả về đúng nếu hàng đợi trống, sai nếu không. Nó tương đương với qsize [] == 0. Returns True if the queue is empty, False otherwise. It is equivalent to qsize[]==0.
- Full [] - Trả về đúng nếu hàng đợi đầy, sai nếu không. Nó tương đương với qsize []> = maxsize. Returns True if the queue is full, False otherwise. It is equivalent to qsize[]>=maxsize.
Lưu ý: empty[]
, full[]
, qsize[]
không đáng tin cậy vì chúng có nguy cơ điều kiện chủng tộc trong đó kích thước hàng đợi có thể thay đổi. empty[]
, full[]
, qsize[]
are not reliable as they risk race condition where the queue size might change.
Example:
from
queue
import
PriorityQueue
q
____10
list of key:value pairs in h: [['g', 2], ['e', 1], ['k', 3], ['s', 4]] pair with lowest priority: ['e', 1] list of keys in h: ['g', 'e', 'k', 's'] list of values in h: [2, 1, 3, 4] remove pair with lowest priority: ['e', 1] get value for key 5 in h: Not found []1
list of key:value pairs in h: [['g', 2], ['e', 1], ['k', 3], ['s', 4]] pair with lowest priority: ['e', 1] list of keys in h: ['g', 'e', 'k', 's'] list of values in h: [2, 1, 3, 4] remove pair with lowest priority: ['e', 1] get value for key 5 in h: Not found []2
list of key:value pairs in h: [['g', 2], ['e', 1], ['k', 3], ['s', 4]] pair with lowest priority: ['e', 1] list of keys in h: ['g', 'e', 'k', 's'] list of values in h: [2, 1, 3, 4] remove pair with lowest priority: ['e', 1] get value for key 5 in h: Not found []3
list of key:value pairs in h: [['g', 2], ['e', 1], ['k', 3], ['s', 4]] pair with lowest priority: ['e', 1] list of keys in h: ['g', 'e', 'k', 's'] list of values in h: [2, 1, 3, 4] remove pair with lowest priority: ['e', 1] get value for key 5 in h: Not found []4
list of key:value pairs in h: [['g', 2], ['e', 1], ['k', 3], ['s', 4]] pair with lowest priority: ['e', 1] list of keys in h: ['g', 'e', 'k', 's'] list of values in h: [2, 1, 3, 4] remove pair with lowest priority: ['e', 1] get value for key 5 in h: Not found []5
list of key:value pairs in h: [['g', 2], ['e', 1], ['k', 3], ['s', 4]] pair with lowest priority: ['e', 1] list of keys in h: ['g', 'e', 'k', 's'] list of values in h: [2, 1, 3, 4] remove pair with lowest priority: ['e', 1] get value for key 5 in h: Not found []6
list of key:value pairs in h: [['g', 2], ['e', 1], ['k', 3], ['s', 4]] pair with lowest priority: ['e', 1] list of keys in h: ['g', 'e', 'k', 's'] list of values in h: [2, 1, 3, 4] remove pair with lowest priority: ['e', 1] get value for key 5 in h: Not found []2
list of key:value pairs in h: [['g', 2], ['e', 1], ['k', 3], ['s', 4]] pair with lowest priority: ['e', 1] list of keys in h: ['g', 'e', 'k', 's'] list of values in h: [2, 1, 3, 4] remove pair with lowest priority: ['e', 1] get value for key 5 in h: Not found []8
list of key:value pairs in h: [['g', 2], ['e', 1], ['k', 3], ['s', 4]] pair with lowest priority: ['e', 1] list of keys in h: ['g', 'e', 'k', 's'] list of values in h: [2, 1, 3, 4] remove pair with lowest priority: ['e', 1] get value for key 5 in h: Not found []4
empty[]
0list of key:value pairs in h: [['g', 2], ['e', 1], ['k', 3], ['s', 4]] pair with lowest priority: ['e', 1] list of keys in h: ['g', 'e', 'k', 's'] list of values in h: [2, 1, 3, 4] remove pair with lowest priority: ['e', 1] get value for key 5 in h: Not found []6
list of key:value pairs in h: [['g', 2], ['e', 1], ['k', 3], ['s', 4]] pair with lowest priority: ['e', 1] list of keys in h: ['g', 'e', 'k', 's'] list of values in h: [2, 1, 3, 4] remove pair with lowest priority: ['e', 1] get value for key 5 in h: Not found []2
empty[]
3list of key:value pairs in h: [['g', 2], ['e', 1], ['k', 3], ['s', 4]] pair with lowest priority: ['e', 1] list of keys in h: ['g', 'e', 'k', 's'] list of values in h: [2, 1, 3, 4] remove pair with lowest priority: ['e', 1] get value for key 5 in h: Not found []4
empty[]
5list of key:value pairs in h: [['g', 2], ['e', 1], ['k', 3], ['s', 4]] pair with lowest priority: ['e', 1] list of keys in h: ['g', 'e', 'k', 's'] list of values in h: [2, 1, 3, 4] remove pair with lowest priority: ['e', 1] get value for key 5 in h: Not found []6
list of key:value pairs in h: [['g', 2], ['e', 1], ['k', 3], ['s', 4]] pair with lowest priority: ['e', 1] list of keys in h: ['g', 'e', 'k', 's'] list of values in h: [2, 1, 3, 4] remove pair with lowest priority: ['e', 1] get value for key 5 in h: Not found []2
empty[]
8list of key:value pairs in h: [['g', 2], ['e', 1], ['k', 3], ['s', 4]] pair with lowest priority: ['e', 1] list of keys in h: ['g', 'e', 'k', 's'] list of values in h: [2, 1, 3, 4] remove pair with lowest priority: ['e', 1] get value for key 5 in h: Not found []4
full[]
0list of key:value pairs in h: [['g', 2], ['e', 1], ['k', 3], ['s', 4]] pair with lowest priority: ['e', 1] list of keys in h: ['g', 'e', 'k', 's'] list of values in h: [2, 1, 3, 4] remove pair with lowest priority: ['e', 1] get value for key 5 in h: Not found []6
list of key:value pairs in h: [['g', 2], ['e', 1], ['k', 3], ['s', 4]] pair with lowest priority: ['e', 1] list of keys in h: ['g', 'e', 'k', 's'] list of values in h: [2, 1, 3, 4] remove pair with lowest priority: ['e', 1] get value for key 5 in h: Not found []2
full[]
3list of key:value pairs in h: [['g', 2], ['e', 1], ['k', 3], ['s', 4]] pair with lowest priority: ['e', 1] list of keys in h: ['g', 'e', 'k', 's'] list of values in h: [2, 1, 3, 4] remove pair with lowest priority: ['e', 1] get value for key 5 in h: Not found []4
empty[]
0list of key:value pairs in h: [['g', 2], ['e', 1], ['k', 3], ['s', 4]] pair with lowest priority: ['e', 1] list of keys in h: ['g', 'e', 'k', 's'] list of values in h: [2, 1, 3, 4] remove pair with lowest priority: ['e', 1] get value for key 5 in h: Not found []6
full[]
7full[]
8
full[]
7full[]
8
full[]
7qsize[]
2qsize[]
3qsize[]
4
full[]
7qsize[]
2qsize[]
7qsize[]
8
full[]
7qsize[]
2from
1from
2
Đầu ra:
[1, 'e'] [2, 'g'] Items in queue : 3 Is queue empty : False Is queue full : False
heapdict[]
Heapdict thực hiện ABC đột biến, có nghĩa là nó hoạt động khá giống như một từ điển Python thông thường. Nó được thiết kế để được sử dụng như một hàng đợi ưu tiên. Cùng với các chức năng được cung cấp bởi from
3 thông thường, nó cũng có các hàm from
4 và from
5 trả về cặp với mức độ ưu tiên thấp nhất. Không giống như mô-đun FEAPQ, FEAPDICT hỗ trợ thay đổi hiệu quả mức độ ưu tiên của một đối tượng hiện có [Khóa giảm dần]. Thay đổi mức độ ưu tiên là rất quan trọng đối với nhiều thuật toán như thuật toán Dijkstra và A*.
Functions-
- Rõ ràng [bản thân] -D.Clear [] -> Không có. Xóa tất cả các mục khỏi D. – D.clear[] -> None. Remove all items from D.
- peekItem [self] -d.peekItem [] -> [k, v], trả về cặp [khóa, giá trị] với giá trị thấp nhất; Nhưng nâng cao keyerror nếu D trống. – D.peekitem[] -> [k, v], return the [key, value] pair with lowest value; but raise KeyError if D is empty.
- PopItem [self] -d.popitem [] -> [k, v], xóa và trả về cặp [giá trị, giá trị] với giá trị thấp nhất; Nhưng nâng cao keyerror nếu D trống. – D.popitem[] -> [k, v], remove and return the [key, value] pair with lowest value; but raise KeyError if D is empty.
- Nhận [tự, khóa, mặc định = Không] -D.Get [k [, d]] -> d [k] nếu k trong d, khác d. D mặc định là không có.] – D.get[k[, d]] -> D[k] if k in D, else d. d defaults to None.
- Các mục [tự] -D.Items [] -> Một đối tượng giống như thiết lập cung cấp chế độ xem trên các mục D– D.items[] -> a set-like object providing a view on D’s items
- Chìa khóa [Tự] -D.Keys [] -> Một đối tượng giống như thiết lập cung cấp chế độ xem trên các phím Diên – D.keys[] -> a set-like object providing a view on D’s keys
- Các giá trị [tự] -D.Values [] -> Một đối tượng cung cấp chế độ xem về các giá trị d. – D.values[] -> an object providing a view on D’s values
Example:
import
from
7
from
8
list of key:value pairs in h: [['g', 2], ['e', 1], ['k', 3], ['s', 4]] pair with lowest priority: ['e', 1] list of keys in h: ['g', 'e', 'k', 's'] list of values in h: [2, 1, 3, 4] remove pair with lowest priority: ['e', 1] get value for key 5 in h: Not found []0
queue
0queue
1
list of key:value pairs in h: [['g', 2], ['e', 1], ['k', 3], ['s', 4]] pair with lowest priority: ['e', 1] list of keys in h: ['g', 'e', 'k', 's'] list of values in h: [2, 1, 3, 4] remove pair with lowest priority: ['e', 1] get value for key 5 in h: Not found []5
queue
3list of key:value pairs in h: [['g', 2], ['e', 1], ['k', 3], ['s', 4]] pair with lowest priority: ['e', 1] list of keys in h: ['g', 'e', 'k', 's'] list of values in h: [2, 1, 3, 4] remove pair with lowest priority: ['e', 1] get value for key 5 in h: Not found []0
list of key:value pairs in h: [['g', 2], ['e', 1], ['k', 3], ['s', 4]] pair with lowest priority: ['e', 1] list of keys in h: ['g', 'e', 'k', 's'] list of values in h: [2, 1, 3, 4] remove pair with lowest priority: ['e', 1] get value for key 5 in h: Not found []3
queue
1empty[]
0queue
3
list of key:value pairs in h: [['g', 2], ['e', 1], ['k', 3], ['s', 4]] pair with lowest priority: ['e', 1] list of keys in h: ['g', 'e', 'k', 's'] list of values in h: [2, 1, 3, 4] remove pair with lowest priority: ['e', 1] get value for key 5 in h: Not found []0
full[]
3queue
1empty[]
5queue
3
list of key:value pairs in h: [['g', 2], ['e', 1], ['k', 3], ['s', 4]] pair with lowest priority: ['e', 1] list of keys in h: ['g', 'e', 'k', 's'] list of values in h: [2, 1, 3, 4] remove pair with lowest priority: ['e', 1] get value for key 5 in h: Not found []0
list of key:value pairs in h: [['g', 2], ['e', 1], ['k', 3], ['s', 4]] pair with lowest priority: ['e', 1] list of keys in h: ['g', 'e', 'k', 's'] list of values in h: [2, 1, 3, 4] remove pair with lowest priority: ['e', 1] get value for key 5 in h: Not found []8
queue
1full[]
03063
list of key:value pairs in h: [['g', 2], ['e', 1], ['k', 3], ['s', 4]] pair with lowest priority: ['e', 1] list of keys in h: ['g', 'e', 'k', 's'] list of values in h: [2, 1, 3, 4] remove pair with lowest priority: ['e', 1] get value for key 5 in h: Not found []0
empty[]
3full[]
7qsize[]
2PriorityQueue
3PriorityQueue
4
PriorityQueue
5PriorityQueue
6PriorityQueue
7
full[]
7qsize[]
2q
0q
1
PriorityQueue
5q
3
full[]
7qsize[]
2q
6q
1
PriorityQueue
5PriorityQueue
6
list of key:value pairs in h: [['g', 2], ['e', 1], ['k', 3], ['s', 4]] pair with lowest priority: ['e', 1] list of keys in h: ['g', 'e', 'k', 's'] list of values in h: [2, 1, 3, 4] remove pair with lowest priority: ['e', 1] get value for key 5 in h: Not found []00
full[]
7qsize[]
2
list of key:value pairs in h: [['g', 2], ['e', 1], ['k', 3], ['s', 4]] pair with lowest priority: ['e', 1] list of keys in h: ['g', 'e', 'k', 's'] list of values in h: [2, 1, 3, 4] remove pair with lowest priority: ['e', 1] get value for key 5 in h: Not found []03
q
1PriorityQueue
5PriorityQueue
6
list of key:value pairs in h: [['g', 2], ['e', 1], ['k', 3], ['s', 4]] pair with lowest priority: ['e', 1] list of keys in h: ['g', 'e', 'k', 's'] list of values in h: [2, 1, 3, 4] remove pair with lowest priority: ['e', 1] get value for key 5 in h: Not found []07
full[]
7qsize[]
2
list of key:value pairs in h: [['g', 2], ['e', 1], ['k', 3], ['s', 4]] pair with lowest priority: ['e', 1] list of keys in h: ['g', 'e', 'k', 's'] list of values in h: [2, 1, 3, 4] remove pair with lowest priority: ['e', 1] get value for key 5 in h: Not found []10
q
1PriorityQueue
5
list of key:value pairs in h: [['g', 2], ['e', 1], ['k', 3], ['s', 4]] pair with lowest priority: ['e', 1] list of keys in h: ['g', 'e', 'k', 's'] list of values in h: [2, 1, 3, 4] remove pair with lowest priority: ['e', 1] get value for key 5 in h: Not found []13
full[]
7qsize[]
2
list of key:value pairs in h: [['g', 2], ['e', 1], ['k', 3], ['s', 4]] pair with lowest priority: ['e', 1] list of keys in h: ['g', 'e', 'k', 's'] list of values in h: [2, 1, 3, 4] remove pair with lowest priority: ['e', 1] get value for key 5 in h: Not found []16
q
1PriorityQueue
5
list of key:value pairs in h: [['g', 2], ['e', 1], ['k', 3], ['s', 4]] pair with lowest priority: ['e', 1] list of keys in h: ['g', 'e', 'k', 's'] list of values in h: [2, 1, 3, 4] remove pair with lowest priority: ['e', 1] get value for key 5 in h: Not found []19
empty[]
8list of key:value pairs in h: [['g', 2], ['e', 1], ['k', 3], ['s', 4]] pair with lowest priority: ['e', 1] list of keys in h: ['g', 'e', 'k', 's'] list of values in h: [2, 1, 3, 4] remove pair with lowest priority: ['e', 1] get value for key 5 in h: Not found []4
list of key:value pairs in h: [['g', 2], ['e', 1], ['k', 3], ['s', 4]] pair with lowest priority: ['e', 1] list of keys in h: ['g', 'e', 'k', 's'] list of values in h: [2, 1, 3, 4] remove pair with lowest priority: ['e', 1] get value for key 5 in h: Not found []22
list of key:value pairs in h: [['g', 2], ['e', 1], ['k', 3], ['s', 4]] pair with lowest priority: ['e', 1] list of keys in h: ['g', 'e', 'k', 's'] list of values in h: [2, 1, 3, 4] remove pair with lowest priority: ['e', 1] get value for key 5 in h: Not found []6
list of key:value pairs in h: [['g', 2], ['e', 1], ['k', 3], ['s', 4]] pair with lowest priority: ['e', 1] list of keys in h: ['g', 'e', 'k', 's'] list of values in h: [2, 1, 3, 4] remove pair with lowest priority: ['e', 1] get value for key 5 in h: Not found []24
full[]
7qsize[]
2PriorityQueue
6PriorityQueue
7
Đầu ra:
list of key:value pairs in h: [['g', 2], ['e', 1], ['k', 3], ['s', 4]] pair with lowest priority: ['e', 1] list of keys in h: ['g', 'e', 'k', 's'] list of values in h: [2, 1, 3, 4] remove pair with lowest priority: ['e', 1] get value for key 5 in h: Not found []