Hướng dẫn how does python priority queue work? - hàng đợi ưu tiên python hoạt động như thế nào?

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
[]
4empty()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
[]
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
[]
2empty()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
[]
4empty()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
[]
2empty()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
[]
4full()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
[]
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
[]
2full()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
[]
4empty()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
[]
6

full()7full()8

full()7full()8

full()7qsize()2qsize()3qsize()4

full()7qsize()2qsize()7qsize()8

full()7qsize()2from1from2

Đầ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 from3 thông thường, nó cũng có các hàm from4 và from5 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 from7

from8

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 0

queue 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
[]
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
[]
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()3

queue 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()3

full()7qsize()2PriorityQueue3PriorityQueue4

PriorityQueue5PriorityQueue6PriorityQueue7

full()7qsize()2q 0q 1

PriorityQueue5q 3

full()7qsize()2q 6q 1

PriorityQueue5PriorityQueue6

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
[]
03q 1

PriorityQueue5PriorityQueue6

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
[]
10q 1

PriorityQueue5

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
[]
16q 1

PriorityQueue5

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
[]
19empty()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
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()2PriorityQueue6PriorityQueue7

Đầ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
[]

Hàng đợi ưu tiên hoạt động như thế nào?

Hàng đợi ưu tiên là một loại hàng đợi đặc biệt trong đó mỗi phần tử được liên kết với giá trị ưu tiên. Và, các yếu tố được phục vụ trên cơ sở ưu tiên của họ. Đó là, các yếu tố ưu tiên cao hơn được phục vụ đầu tiên. Tuy nhiên, nếu các yếu tố có cùng mức độ ưu tiên xảy ra, chúng được phục vụ theo thứ tự của chúng trong hàng đợi.each element is associated with a priority value. And, elements are served on the basis of their priority. That is, higher priority elements are served first. However, if elements with the same priority occur, they are served according to their order in the queue.

Python đã xây dựng trong hàng đợi ưu tiên?

Việc triển khai lớp ưu tiên của Python mở rộng mô -đun Python Heapq, dựa trên thiết kế đống nhị phân.Rất dễ dàng để lấy và loại bỏ vật phẩm ưu tiên cao nhất khỏi một đống nhị phân.

Hàng đợi Python hoạt động như thế nào?

Giống như Stack, Hàng đợi là một cấu trúc dữ liệu tuyến tính lưu trữ các mục theo cách đầu tiên theo cách đầu tiên (FIFO).Với một hàng đợi, mục được thêm gần đây nhất được loại bỏ trước tiên.Một ví dụ điển hình về hàng đợi là bất kỳ hàng đợi người tiêu dùng nào cho một tài nguyên nơi người tiêu dùng đến trước được phục vụ trước.With a queue the least recently added item is removed first. A good example of queue is any queue of consumers for a resource where the consumer that came first is served first.

Làm cách nào để đặt hàng hàng đợi ưu tiên trong Python?

Có hai cách để thực hiện hàng đợi ưu tiên trong Python: sử dụng lớp hàng đợi và sử dụng mô -đun FEAPQ.Bạn có thể muốn đặt hàng dữ liệu dựa trên các giá trị của từng mục trong danh sách.Chẳng hạn, bạn có thể muốn giá trị cao nhất xuất hiện đầu tiên trong danh sách và giá trị thấp nhất xuất hiện cuối cùng trong danh sách.using the queue class and using the heapq module. You may want to order data based on the values of each item in the list. For instance, you may want the highest value to appear first in the list, and the lowest value to appear last in the list.