Hướng dẫn python priority queue order - thứ tự hàng đợi ưu tiên python

from queue import PriorityQueue

q = PriorityQueue()

q.put((2, 'code'))
q.put((1, 'eat'))
q.put((3, 'sleep'))

while not q.empty():
    next_item = q.get()
    print(next_item)

# Result:
#   (1, 'eat')
#   (2, 'code')
#   (3, 'sleep')

Tôi có thể làm gì để nó bật theo thứ tự ngược lại? tức là giảm dần

Nội dung chính ShowShow

  • Làm thế nào để tôi có được hàng đợi ưu tiên của tôi theo thứ tự ngược lại?
  • Làm thế nào để bạn đảo ngược một hàng đợi ưu tiên trong Python?
  • Làm cách nào để đặt hàng hàng đợi ưu tiên trong Python?
  • Làm thế nào để bạn tạo một danh sách ưu tiên trong Python?

Đã hỏi ngày 26 tháng 6 năm 2019 lúc 23:11Jun 26, 2019 at 23:11Jun 26, 2019 at 23:11

3

Bạn có thể sử dụng phương pháp này như một thủ thuật nhân với -1 để ngược lại thứ tự:

q = PriorityQueue()

q.put((2*(-1), 'code'))
q.put((1*(-1), 'eat'))
q.put((3*(-1), 'sleep'))
while not q.empty():
    next_item = q.get()
    print(next_item)

output:

(-3, 'sleep')
(-2, 'code')
(-1, 'eat')

Đã trả lời ngày 26 tháng 6 năm 2019 lúc 23:31Jun 26, 2019 at 23:31Jun 26, 2019 at 23:31

GhassenghassenGhassenGhassen

7536 Huy hiệu bạc14 Huy hiệu đồng6 silver badges14 bronze badges6 silver badges14 bronze badges

2

Không có cách nào để thay đổi hành vi của lớp hàng đợi. Tôi sẽ trang trí các vật phẩm trước khi xếp hàng chúng và không giải mã chúng sau khi lấy chúng, một cái gì đó như thế này:

def decorated(item):
    return (-item[0],) + item

def undecorated(item):
    return item[1:]

Điều đó cung cấp cho bạn các mục sẽ xếp hàng theo đúng thứ tự:

>>> decorated(1, 'eat')
(-1, 1, 'eat')
>>> undecorated(-1, 1, 'eat')
(1, 'eat')

Bằng cách đó, bạn có thể nói

(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
43 thay vì
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
44. Tương tự, bạn lấy lại vật phẩm bằng cách nói
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
45 thay vì

q = PriorityQueue()

q.put((2*(-1), 'code'))
q.put((1*(-1), 'eat'))
q.put((3*(-1), 'sleep'))
while not q.empty():
    next_item = q.get()
    print(next_item)
0. Sau đó, phần còn lại của mã của bạn tiếp tục hoạt động như trước đây.

Đã trả lời ngày 27 tháng 6 năm 2019 lúc 0:00Jun 27, 2019 at 0:00Jun 27, 2019 at 0:00

API

q = PriorityQueue()

q.put((2*(-1), 'code'))
q.put((1*(-1), 'eat'))
q.put((3*(-1), 'sleep'))
while not q.empty():
    next_item = q.get()
    print(next_item)
1 cho phép bạn xác định thứ tự.

Nếu bạn muốn các chuỗi đó theo thứ tự ngược lại, chỉ cần đảo ngược các giá trị

q = PriorityQueue()

q.put((2*(-1), 'code'))
q.put((1*(-1), 'eat'))
q.put((3*(-1), 'sleep'))
while not q.empty():
    next_item = q.get()
    print(next_item)
2 trong các bộ dữ liệu:
from queue import PriorityQueue

q = PriorityQueue()

q.put((2, 'code'))
q.put((3, 'eat'))
q.put((1, 'sleep'))

while not q.empty():
    next_item = q.get()
    print(next_item)

#(1, 'sleep')
#(2, 'code')
#(3, 'eat')

Không có cách nào để đảo ngược hành vi của

q = PriorityQueue()

q.put((2*(-1), 'code'))
q.put((1*(-1), 'eat'))
q.put((3*(-1), 'sleep'))
while not q.empty():
    next_item = q.get()
    print(next_item)
1 và Python (không phải là Thể thông)
q = PriorityQueue()

q.put((2*(-1), 'code'))
q.put((1*(-1), 'eat'))
q.put((3*(-1), 'sleep'))
while not q.empty():
    next_item = q.get()
    print(next_item)
4 sử dụng cùng một thứ tự. Nhưng tôi thấy quan điểm của bạn, tên thật có lẽ là "
q = PriorityQueue()

q.put((2*(-1), 'code'))
q.put((1*(-1), 'eat'))
q.put((3*(-1), 'sleep'))
while not q.empty():
    next_item = q.get()
    print(next_item)
5" vì
q = PriorityQueue()

q.put((2*(-1), 'code'))
q.put((1*(-1), 'eat'))
q.put((3*(-1), 'sleep'))
while not q.empty():
    next_item = q.get()
    print(next_item)
6 luôn là lần đầu tiên trong cấu trúc này.

Đã trả lời ngày 26 tháng 6 năm 2019 lúc 23:20Jun 26, 2019 at 23:20Jun 26, 2019 at 23:20

JacobirrjacobirrJacobIRRJacobIRR

8.0807 Huy hiệu vàng35 Huy hiệu bạc60 Huy hiệu Đồng7 gold badges35 silver badges60 bronze badges7 gold badges35 silver badges60 bronze badges

1

Biểu thức Lambda Kể từ khi Java 8 đã sử dụng, hàm Lambda đặt tên cho các tham số đầu vào của nó A và B và Returns (B-A), về cơ bản là những gì lớp so sánh INT làm ngoại trừ nó trả về A-B.

Declaration:

q = PriorityQueue()

q.put((2*(-1), 'code'))
q.put((1*(-1), 'eat'))
q.put((3*(-1), 'sleep'))
while not q.empty():
    next_item = q.get()
    print(next_item)
3

Ví dụ: & nbsp;E is the type of elements held in this queueE is the type of elements held in this queue

q = PriorityQueue()

q.put((2*(-1), 'code'))
q.put((1*(-1), 'eat'))
q.put((3*(-1), 'sleep'))
while not q.empty():
    next_item = q.get()
    print(next_item)
49
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
09
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
0

  • (-3, 'sleep')
    (-2, 'code')
    (-1, 'eat')
    
    1
    (-3, 'sleep')
    (-2, 'code')
    (-1, 'eat')
    
    2
    (-3, 'sleep')
    (-2, 'code')
    (-1, 'eat')
    
    3
    (-3, 'sleep')
    (-2, 'code')
    (-1, 'eat')
    
    4
    (-3, 'sleep')
    (-2, 'code')
    (-1, 'eat')
    
    5
  • def decorated(item):
        return (-item[0],) + item
    
    def undecorated(item):
        return item[1:]
    
    0
    def decorated(item):
        return (-item[0],) + item
    
    def undecorated(item):
        return item[1:]
    
    1
    def decorated(item):
        return (-item[0],) + item
    
    def undecorated(item):
        return item[1:]
    
    2
    q = PriorityQueue()
    
    q.put((2*(-1), 'code'))
    q.put((1*(-1), 'eat'))
    q.put((3*(-1), 'sleep'))
    while not q.empty():
        next_item = q.get()
        print(next_item)
    
    94

Làm thế nào để tôi có được hàng đợi ưu tiên của tôi theo thứ tự ngược lại?

Java

q = PriorityQueue()

q.put((2*(-1), 'code'))
q.put((1*(-1), 'eat'))
q.put((3*(-1), 'sleep'))
while not q.empty():
    next_item = q.get()
    print(next_item)
7
q = PriorityQueue()

q.put((2*(-1), 'code'))
q.put((1*(-1), 'eat'))
q.put((3*(-1), 'sleep'))
while not q.empty():
    next_item = q.get()
    print(next_item)
8
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
2
q = PriorityQueue()

q.put((2*(-1), 'code'))
q.put((1*(-1), 'eat'))
q.put((3*(-1), 'sleep'))
while not q.empty():
    next_item = q.get()
    print(next_item)
9
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
0
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
1
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
2
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
3
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
4
q = PriorityQueue()

q.put((2*(-1), 'code'))
q.put((1*(-1), 'eat'))
q.put((3*(-1), 'sleep'))
while not q.empty():
    next_item = q.get()
    print(next_item)
12
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
1
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
7
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
8
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
9
def decorated(item):
    return (-item[0],) + item

def undecorated(item):
    return item[1:]
0
def decorated(item):
    return (-item[0],) + item

def undecorated(item):
    return item[1:]
1
def decorated(item):
    return (-item[0],) + item

def undecorated(item):
    return item[1:]
2
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
435

(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
436

def decorated(item):
    return (-item[0],) + item

def undecorated(item):
    return item[1:]
2
q = PriorityQueue()

q.put((2*(-1), 'code'))
q.put((1*(-1), 'eat'))
q.put((3*(-1), 'sleep'))
while not q.empty():
    next_item = q.get()
    print(next_item)
23
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
8
def decorated(item):
    return (-item[0],) + item

def undecorated(item):
    return item[1:]
5
>>> decorated(1, 'eat')
(-1, 1, 'eat')
>>> undecorated(-1, 1, 'eat')
(1, 'eat')
0
def decorated(item):
    return (-item[0],) + item

def undecorated(item):
    return item[1:]
7
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
8
def decorated(item):
    return (-item[0],) + item

def undecorated(item):
    return item[1:]
5
>>> decorated(1, 'eat')
(-1, 1, 'eat')
>>> undecorated(-1, 1, 'eat')
(1, 'eat')
4
def decorated(item):
    return (-item[0],) + item

def undecorated(item):
    return item[1:]
7
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
8
def decorated(item):
    return (-item[0],) + item

def undecorated(item):
    return item[1:]
5
>>> decorated(1, 'eat')
(-1, 1, 'eat')
>>> undecorated(-1, 1, 'eat')
(1, 'eat')
8
def decorated(item):
    return (-item[0],) + item

def undecorated(item):
    return item[1:]
7
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
8
from queue import PriorityQueue

q = PriorityQueue()

q.put((2, 'code'))
q.put((3, 'eat'))
q.put((1, 'sleep'))

while not q.empty():
    next_item = q.get()
    print(next_item)

#(1, 'sleep')
#(2, 'code')
#(3, 'eat')
1
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
8
from queue import PriorityQueue

q = PriorityQueue()

q.put((2, 'code'))
q.put((3, 'eat'))
q.put((1, 'sleep'))

while not q.empty():
    next_item = q.get()
    print(next_item)

#(1, 'sleep')
#(2, 'code')
#(3, 'eat')
3
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
8
from queue import PriorityQueue

q = PriorityQueue()

q.put((2, 'code'))
q.put((3, 'eat'))
q.put((1, 'sleep'))

while not q.empty():
    next_item = q.get()
    print(next_item)

#(1, 'sleep')
#(2, 'code')
#(3, 'eat')
1
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
1
from queue import PriorityQueue

q = PriorityQueue()

q.put((2, 'code'))
q.put((3, 'eat'))
q.put((1, 'sleep'))

while not q.empty():
    next_item = q.get()
    print(next_item)

#(1, 'sleep')
#(2, 'code')
#(3, 'eat')
7
from queue import PriorityQueue

q = PriorityQueue()

q.put((2, 'code'))
q.put((3, 'eat'))
q.put((1, 'sleep'))

while not q.empty():
    next_item = q.get()
    print(next_item)

#(1, 'sleep')
#(2, 'code')
#(3, 'eat')
7
q = PriorityQueue()

q.put((2*(-1), 'code'))
q.put((1*(-1), 'eat'))
q.put((3*(-1), 'sleep'))
while not q.empty():
    next_item = q.get()
    print(next_item)
24
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
2
q = PriorityQueue()

q.put((2*(-1), 'code'))
q.put((1*(-1), 'eat'))
q.put((3*(-1), 'sleep'))
while not q.empty():
    next_item = q.get()
    print(next_item)
2
q = PriorityQueue()

q.put((2*(-1), 'code'))
q.put((1*(-1), 'eat'))
q.put((3*(-1), 'sleep'))
while not q.empty():
    next_item = q.get()
    print(next_item)
27
  • q = PriorityQueue()
    
    q.put((2*(-1), 'code'))
    q.put((1*(-1), 'eat'))
    q.put((3*(-1), 'sleep'))
    while not q.empty():
        next_item = q.get()
        print(next_item)
    
    30
    q = PriorityQueue()
    
    q.put((2*(-1), 'code'))
    q.put((1*(-1), 'eat'))
    q.put((3*(-1), 'sleep'))
    while not q.empty():
        next_item = q.get()
        print(next_item)
    
    31
    q = PriorityQueue()
    
    q.put((2*(-1), 'code'))
    q.put((1*(-1), 'eat'))
    q.put((3*(-1), 'sleep'))
    while not q.empty():
        next_item = q.get()
        print(next_item)
    
    32
  • q = PriorityQueue()
    
    q.put((2*(-1), 'code'))
    q.put((1*(-1), 'eat'))
    q.put((3*(-1), 'sleep'))
    while not q.empty():
        next_item = q.get()
        print(next_item)
    
    33
    q = PriorityQueue()
    
    q.put((2*(-1), 'code'))
    q.put((1*(-1), 'eat'))
    q.put((3*(-1), 'sleep'))
    while not q.empty():
        next_item = q.get()
        print(next_item)
    
    34
    q = PriorityQueue()
    
    q.put((2*(-1), 'code'))
    q.put((1*(-1), 'eat'))
    q.put((3*(-1), 'sleep'))
    while not q.empty():
        next_item = q.get()
        print(next_item)
    
    6
    q = PriorityQueue()
    
    q.put((2*(-1), 'code'))
    q.put((1*(-1), 'eat'))
    q.put((3*(-1), 'sleep'))
    while not q.empty():
        next_item = q.get()
        print(next_item)
    
    36
  • q = PriorityQueue()
    
    q.put((2*(-1), 'code'))
    q.put((1*(-1), 'eat'))
    q.put((3*(-1), 'sleep'))
    while not q.empty():
        next_item = q.get()
        print(next_item)
    
    30
    q = PriorityQueue()
    
    q.put((2*(-1), 'code'))
    q.put((1*(-1), 'eat'))
    q.put((3*(-1), 'sleep'))
    while not q.empty():
        next_item = q.get()
        print(next_item)
    
    31
    q = PriorityQueue()
    
    q.put((2*(-1), 'code'))
    q.put((1*(-1), 'eat'))
    q.put((3*(-1), 'sleep'))
    while not q.empty():
        next_item = q.get()
        print(next_item)
    
    39lambda expressionlambda expression

q = PriorityQueue()

q.put((2*(-1), 'code'))
q.put((1*(-1), 'eat'))
q.put((3*(-1), 'sleep'))
while not q.empty():
    next_item = q.get()
    print(next_item)
433
q = PriorityQueue()

q.put((2*(-1), 'code'))
q.put((1*(-1), 'eat'))
q.put((3*(-1), 'sleep'))
while not q.empty():
    next_item = q.get()
    print(next_item)
34
q = PriorityQueue()

q.put((2*(-1), 'code'))
q.put((1*(-1), 'eat'))
q.put((3*(-1), 'sleep'))
while not q.empty():
    next_item = q.get()
    print(next_item)
42
q = PriorityQueue()

q.put((2*(-1), 'code'))
q.put((1*(-1), 'eat'))
q.put((3*(-1), 'sleep'))
while not q.empty():
    next_item = q.get()
    print(next_item)
6
q = PriorityQueue()

q.put((2*(-1), 'code'))
q.put((1*(-1), 'eat'))
q.put((3*(-1), 'sleep'))
while not q.empty():
    next_item = q.get()
    print(next_item)
36Using default Comparator Collections.reverseOrder()33
q = PriorityQueue()

q.put((2*(-1), 'code'))
q.put((1*(-1), 'eat'))
q.put((3*(-1), 'sleep'))
while not q.empty():
    next_item = q.get()
    print(next_item)
34
q = PriorityQueue()

q.put((2*(-1), 'code'))
q.put((1*(-1), 'eat'))
q.put((3*(-1), 'sleep'))
while not q.empty():
    next_item = q.get()
    print(next_item)
42
q = PriorityQueue()

q.put((2*(-1), 'code'))
q.put((1*(-1), 'eat'))
q.put((3*(-1), 'sleep'))
while not q.empty():
    next_item = q.get()
    print(next_item)
6
q = PriorityQueue()

q.put((2*(-1), 'code'))
q.put((1*(-1), 'eat'))
q.put((3*(-1), 'sleep'))
while not q.empty():
    next_item = q.get()
    print(next_item)
36
Using default Comparator Collections.reverseOrder()

q = PriorityQueue()

q.put((2*(-1), 'code'))
q.put((1*(-1), 'eat'))
q.put((3*(-1), 'sleep'))
while not q.empty():
    next_item = q.get()
    print(next_item)
30
q = PriorityQueue()

q.put((2*(-1), 'code'))
q.put((1*(-1), 'eat'))
q.put((3*(-1), 'sleep'))
while not q.empty():
    next_item = q.get()
    print(next_item)
34
q = PriorityQueue()

q.put((2*(-1), 'code'))
q.put((1*(-1), 'eat'))
q.put((3*(-1), 'sleep'))
while not q.empty():
    next_item = q.get()
    print(next_item)
47
q = PriorityQueue()

q.put((2*(-1), 'code'))
q.put((1*(-1), 'eat'))
q.put((3*(-1), 'sleep'))
while not q.empty():
    next_item = q.get()
    print(next_item)
36Collections.reverseOrder() method is used to get a reverse behavior of the default comparator. This is a by default comparator injava.util package.Collections.reverseOrder() method is used to get a reverse behavior of the default comparator. This is a by default comparator injava.util package.

Example:   

Java

q = PriorityQueue()

q.put((2*(-1), 'code'))
q.put((1*(-1), 'eat'))
q.put((3*(-1), 'sleep'))
while not q.empty():
    next_item = q.get()
    print(next_item)
7
q = PriorityQueue()

q.put((2*(-1), 'code'))
q.put((1*(-1), 'eat'))
q.put((3*(-1), 'sleep'))
while not q.empty():
    next_item = q.get()
    print(next_item)
8
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
2
q = PriorityQueue()

q.put((2*(-1), 'code'))
q.put((1*(-1), 'eat'))
q.put((3*(-1), 'sleep'))
while not q.empty():
    next_item = q.get()
    print(next_item)
9
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
0
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
1
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
2
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
3
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
4
q = PriorityQueue()

q.put((2*(-1), 'code'))
q.put((1*(-1), 'eat'))
q.put((3*(-1), 'sleep'))
while not q.empty():
    next_item = q.get()
    print(next_item)
12
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
1
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
7
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
8
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
9
def decorated(item):
    return (-item[0],) + item

def undecorated(item):
    return item[1:]
0
def decorated(item):
    return (-item[0],) + item

def undecorated(item):
    return item[1:]
1
def decorated(item):
    return (-item[0],) + item

def undecorated(item):
    return item[1:]
2
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
435

(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
436
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
437

def decorated(item):
    return (-item[0],) + item

def undecorated(item):
    return item[1:]
2
q = PriorityQueue()

q.put((2*(-1), 'code'))
q.put((1*(-1), 'eat'))
q.put((3*(-1), 'sleep'))
while not q.empty():
    next_item = q.get()
    print(next_item)
23
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
8
def decorated(item):
    return (-item[0],) + item

def undecorated(item):
    return item[1:]
5
>>> decorated(1, 'eat')
(-1, 1, 'eat')
>>> undecorated(-1, 1, 'eat')
(1, 'eat')
0
def decorated(item):
    return (-item[0],) + item

def undecorated(item):
    return item[1:]
7
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
8
def decorated(item):
    return (-item[0],) + item

def undecorated(item):
    return item[1:]
5
>>> decorated(1, 'eat')
(-1, 1, 'eat')
>>> undecorated(-1, 1, 'eat')
(1, 'eat')
4
def decorated(item):
    return (-item[0],) + item

def undecorated(item):
    return item[1:]
7
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
8
def decorated(item):
    return (-item[0],) + item

def undecorated(item):
    return item[1:]
5
>>> decorated(1, 'eat')
(-1, 1, 'eat')
>>> undecorated(-1, 1, 'eat')
(1, 'eat')
8
def decorated(item):
    return (-item[0],) + item

def undecorated(item):
    return item[1:]
7
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
8
from queue import PriorityQueue

q = PriorityQueue()

q.put((2, 'code'))
q.put((3, 'eat'))
q.put((1, 'sleep'))

while not q.empty():
    next_item = q.get()
    print(next_item)

#(1, 'sleep')
#(2, 'code')
#(3, 'eat')
1
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
8
from queue import PriorityQueue

q = PriorityQueue()

q.put((2, 'code'))
q.put((3, 'eat'))
q.put((1, 'sleep'))

while not q.empty():
    next_item = q.get()
    print(next_item)

#(1, 'sleep')
#(2, 'code')
#(3, 'eat')
3
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
8
from queue import PriorityQueue

q = PriorityQueue()

q.put((2, 'code'))
q.put((3, 'eat'))
q.put((1, 'sleep'))

while not q.empty():
    next_item = q.get()
    print(next_item)

#(1, 'sleep')
#(2, 'code')
#(3, 'eat')
1
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
1
from queue import PriorityQueue

q = PriorityQueue()

q.put((2, 'code'))
q.put((3, 'eat'))
q.put((1, 'sleep'))

while not q.empty():
    next_item = q.get()
    print(next_item)

#(1, 'sleep')
#(2, 'code')
#(3, 'eat')
7
from queue import PriorityQueue

q = PriorityQueue()

q.put((2, 'code'))
q.put((3, 'eat'))
q.put((1, 'sleep'))

while not q.empty():
    next_item = q.get()
    print(next_item)

#(1, 'sleep')
#(2, 'code')
#(3, 'eat')
7
q = PriorityQueue()

q.put((2*(-1), 'code'))
q.put((1*(-1), 'eat'))
q.put((3*(-1), 'sleep'))
while not q.empty():
    next_item = q.get()
    print(next_item)
24
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
2
q = PriorityQueue()

q.put((2*(-1), 'code'))
q.put((1*(-1), 'eat'))
q.put((3*(-1), 'sleep'))
while not q.empty():
    next_item = q.get()
    print(next_item)
2
q = PriorityQueue()

q.put((2*(-1), 'code'))
q.put((1*(-1), 'eat'))
q.put((3*(-1), 'sleep'))
while not q.empty():
    next_item = q.get()
    print(next_item)
27

q = PriorityQueue()

q.put((2*(-1), 'code'))
q.put((1*(-1), 'eat'))
q.put((3*(-1), 'sleep'))
while not q.empty():
    next_item = q.get()
    print(next_item)
30
q = PriorityQueue()

q.put((2*(-1), 'code'))
q.put((1*(-1), 'eat'))
q.put((3*(-1), 'sleep'))
while not q.empty():
    next_item = q.get()
    print(next_item)
31
q = PriorityQueue()

q.put((2*(-1), 'code'))
q.put((1*(-1), 'eat'))
q.put((3*(-1), 'sleep'))
while not q.empty():
    next_item = q.get()
    print(next_item)
32

q = PriorityQueue()

q.put((2*(-1), 'code'))
q.put((1*(-1), 'eat'))
q.put((3*(-1), 'sleep'))
while not q.empty():
    next_item = q.get()
    print(next_item)
33
q = PriorityQueue()

q.put((2*(-1), 'code'))
q.put((1*(-1), 'eat'))
q.put((3*(-1), 'sleep'))
while not q.empty():
    next_item = q.get()
    print(next_item)
34
q = PriorityQueue()

q.put((2*(-1), 'code'))
q.put((1*(-1), 'eat'))
q.put((3*(-1), 'sleep'))
while not q.empty():
    next_item = q.get()
    print(next_item)
6
q = PriorityQueue()

q.put((2*(-1), 'code'))
q.put((1*(-1), 'eat'))
q.put((3*(-1), 'sleep'))
while not q.empty():
    next_item = q.get()
    print(next_item)
36java.util.PriorityQueue.comparator() method shares an important function of setting and returning the comparator that can be used to order the elements in a PriorityQueue. The method returns a null value if the queue follows the natural ordering pattern of the elements.

Example:   

Java

q = PriorityQueue()

q.put((2*(-1), 'code'))
q.put((1*(-1), 'eat'))
q.put((3*(-1), 'sleep'))
while not q.empty():
    next_item = q.get()
    print(next_item)
7
q = PriorityQueue()

q.put((2*(-1), 'code'))
q.put((1*(-1), 'eat'))
q.put((3*(-1), 'sleep'))
while not q.empty():
    next_item = q.get()
    print(next_item)
8
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
2
q = PriorityQueue()

q.put((2*(-1), 'code'))
q.put((1*(-1), 'eat'))
q.put((3*(-1), 'sleep'))
while not q.empty():
    next_item = q.get()
    print(next_item)
9
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
0
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
1
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
2
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
3
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
4
q = PriorityQueue()

q.put((2*(-1), 'code'))
q.put((1*(-1), 'eat'))
q.put((3*(-1), 'sleep'))
while not q.empty():
    next_item = q.get()
    print(next_item)
12
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
1
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
7
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
8
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
9
def decorated(item):
    return (-item[0],) + item

def undecorated(item):
    return item[1:]
0
def decorated(item):
    return (-item[0],) + item

def undecorated(item):
    return item[1:]
1
def decorated(item):
    return (-item[0],) + item

def undecorated(item):
    return item[1:]
2
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
435

(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
436

def decorated(item):
    return (-item[0],) + item

def undecorated(item):
    return item[1:]
2
q = PriorityQueue()

q.put((2*(-1), 'code'))
q.put((1*(-1), 'eat'))
q.put((3*(-1), 'sleep'))
while not q.empty():
    next_item = q.get()
    print(next_item)
23
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
8
def decorated(item):
    return (-item[0],) + item

def undecorated(item):
    return item[1:]
5
>>> decorated(1, 'eat')
(-1, 1, 'eat')
>>> undecorated(-1, 1, 'eat')
(1, 'eat')
0
def decorated(item):
    return (-item[0],) + item

def undecorated(item):
    return item[1:]
7
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
8
def decorated(item):
    return (-item[0],) + item

def undecorated(item):
    return item[1:]
5
>>> decorated(1, 'eat')
(-1, 1, 'eat')
>>> undecorated(-1, 1, 'eat')
(1, 'eat')
4
def decorated(item):
    return (-item[0],) + item

def undecorated(item):
    return item[1:]
7
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
8
def decorated(item):
    return (-item[0],) + item

def undecorated(item):
    return item[1:]
5
>>> decorated(1, 'eat')
(-1, 1, 'eat')
>>> undecorated(-1, 1, 'eat')
(1, 'eat')
8
def decorated(item):
    return (-item[0],) + item

def undecorated(item):
    return item[1:]
7
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
8
from queue import PriorityQueue

q = PriorityQueue()

q.put((2, 'code'))
q.put((3, 'eat'))
q.put((1, 'sleep'))

while not q.empty():
    next_item = q.get()
    print(next_item)

#(1, 'sleep')
#(2, 'code')
#(3, 'eat')
1
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
8
from queue import PriorityQueue

q = PriorityQueue()

q.put((2, 'code'))
q.put((3, 'eat'))
q.put((1, 'sleep'))

while not q.empty():
    next_item = q.get()
    print(next_item)

#(1, 'sleep')
#(2, 'code')
#(3, 'eat')
3
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
8
from queue import PriorityQueue

q = PriorityQueue()

q.put((2, 'code'))
q.put((3, 'eat'))
q.put((1, 'sleep'))

while not q.empty():
    next_item = q.get()
    print(next_item)

#(1, 'sleep')
#(2, 'code')
#(3, 'eat')
1
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
1
from queue import PriorityQueue

q = PriorityQueue()

q.put((2, 'code'))
q.put((3, 'eat'))
q.put((1, 'sleep'))

while not q.empty():
    next_item = q.get()
    print(next_item)

#(1, 'sleep')
#(2, 'code')
#(3, 'eat')
7
from queue import PriorityQueue

q = PriorityQueue()

q.put((2, 'code'))
q.put((3, 'eat'))
q.put((1, 'sleep'))

while not q.empty():
    next_item = q.get()
    print(next_item)

#(1, 'sleep')
#(2, 'code')
#(3, 'eat')
7
q = PriorityQueue()

q.put((2*(-1), 'code'))
q.put((1*(-1), 'eat'))
q.put((3*(-1), 'sleep'))
while not q.empty():
    next_item = q.get()
    print(next_item)
24
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
2
q = PriorityQueue()

q.put((2*(-1), 'code'))
q.put((1*(-1), 'eat'))
q.put((3*(-1), 'sleep'))
while not q.empty():
    next_item = q.get()
    print(next_item)
2
q = PriorityQueue()

q.put((2*(-1), 'code'))
q.put((1*(-1), 'eat'))
q.put((3*(-1), 'sleep'))
while not q.empty():
    next_item = q.get()
    print(next_item)
27

(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
436

q = PriorityQueue()

q.put((2*(-1), 'code'))
q.put((1*(-1), 'eat'))
q.put((3*(-1), 'sleep'))
while not q.empty():
    next_item = q.get()
    print(next_item)
52
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
8
def decorated(item):
    return (-item[0],) + item

def undecorated(item):
    return item[1:]
5
def decorated(item):
    return (-item[0],) + item

def undecorated(item):
    return item[1:]
6
def decorated(item):
    return (-item[0],) + item

def undecorated(item):
    return item[1:]
7
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
8
def decorated(item):
    return (-item[0],) + item

def undecorated(item):
    return item[1:]
5
>>> decorated(1, 'eat')
(-1, 1, 'eat')
>>> undecorated(-1, 1, 'eat')
(1, 'eat')
4
def decorated(item):
    return (-item[0],) + item

def undecorated(item):
    return item[1:]
7
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
8
def decorated(item):
    return (-item[0],) + item

def undecorated(item):
    return item[1:]
5
>>> decorated(1, 'eat')
(-1, 1, 'eat')
>>> undecorated(-1, 1, 'eat')
(1, 'eat')
0
def decorated(item):
    return (-item[0],) + item

def undecorated(item):
    return item[1:]
7
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
8
def decorated(item):
    return (-item[0],) + item

def undecorated(item):
    return item[1:]
5
>>> decorated(1, 'eat')
(-1, 1, 'eat')
>>> undecorated(-1, 1, 'eat')
(1, 'eat')
8
def decorated(item):
    return (-item[0],) + item

def undecorated(item):
    return item[1:]
7
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
8
from queue import PriorityQueue

q = PriorityQueue()

q.put((2, 'code'))
q.put((3, 'eat'))
q.put((1, 'sleep'))

while not q.empty():
    next_item = q.get()
    print(next_item)

#(1, 'sleep')
#(2, 'code')
#(3, 'eat')
1
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
8
from queue import PriorityQueue

q = PriorityQueue()

q.put((2, 'code'))
q.put((3, 'eat'))
q.put((1, 'sleep'))

while not q.empty():
    next_item = q.get()
    print(next_item)

#(1, 'sleep')
#(2, 'code')
#(3, 'eat')
3
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
8
from queue import PriorityQueue

q = PriorityQueue()

q.put((2, 'code'))
q.put((3, 'eat'))
q.put((1, 'sleep'))

while not q.empty():
    next_item = q.get()
    print(next_item)

#(1, 'sleep')
#(2, 'code')
#(3, 'eat')
1
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
1
from queue import PriorityQueue

q = PriorityQueue()

q.put((2, 'code'))
q.put((3, 'eat'))
q.put((1, 'sleep'))

while not q.empty():
    next_item = q.get()
    print(next_item)

#(1, 'sleep')
#(2, 'code')
#(3, 'eat')
7
from queue import PriorityQueue

q = PriorityQueue()

q.put((2, 'code'))
q.put((3, 'eat'))
q.put((1, 'sleep'))

while not q.empty():
    next_item = q.get()
    print(next_item)

#(1, 'sleep')
#(2, 'code')
#(3, 'eat')
7

Phương pháp 3: Sử dụng biểu thức Lambdalambda expressionlambda expression

Biểu thức Lambda Kể từ khi Java 8 đã sử dụng, hàm Lambda đặt tên cho các tham số đầu vào của nó A và B và Returns (B-A), về cơ bản là những gì lớp so sánh INT làm ngoại trừ nó trả về A-B. since Java 8 has come to use, lambda function names its input parameters a and b and returns (b-a), which is basically what the int comparator class does except it returns a-b. since Java 8 has come to use, lambda function names its input parameters a and b and returns (b-a), which is basically what the int comparator class does except it returns a-b.

Ví dụ: & nbsp;   

Java

q = PriorityQueue()

q.put((2*(-1), 'code'))
q.put((1*(-1), 'eat'))
q.put((3*(-1), 'sleep'))
while not q.empty():
    next_item = q.get()
    print(next_item)
7
q = PriorityQueue()

q.put((2*(-1), 'code'))
q.put((1*(-1), 'eat'))
q.put((3*(-1), 'sleep'))
while not q.empty():
    next_item = q.get()
    print(next_item)
8
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
2
q = PriorityQueue()

q.put((2*(-1), 'code'))
q.put((1*(-1), 'eat'))
q.put((3*(-1), 'sleep'))
while not q.empty():
    next_item = q.get()
    print(next_item)
9
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
0
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
1
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
2
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
3
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
4
q = PriorityQueue()

q.put((2*(-1), 'code'))
q.put((1*(-1), 'eat'))
q.put((3*(-1), 'sleep'))
while not q.empty():
    next_item = q.get()
    print(next_item)
12
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
1
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
7
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
8
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
9

(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
436

def decorated(item):
    return (-item[0],) + item

def undecorated(item):
    return item[1:]
2
q = PriorityQueue()

q.put((2*(-1), 'code'))
q.put((1*(-1), 'eat'))
q.put((3*(-1), 'sleep'))
while not q.empty():
    next_item = q.get()
    print(next_item)
23
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
8
def decorated(item):
    return (-item[0],) + item

def undecorated(item):
    return item[1:]
5
def decorated(item):
    return (-item[0],) + item

def undecorated(item):
    return item[1:]
6
def decorated(item):
    return (-item[0],) + item

def undecorated(item):
    return item[1:]
7
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
8
def decorated(item):
    return (-item[0],) + item

def undecorated(item):
    return item[1:]
5
>>> decorated(1, 'eat')
(-1, 1, 'eat')
>>> undecorated(-1, 1, 'eat')
(1, 'eat')
0
def decorated(item):
    return (-item[0],) + item

def undecorated(item):
    return item[1:]
7
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
8
def decorated(item):
    return (-item[0],) + item

def undecorated(item):
    return item[1:]
5
>>> decorated(1, 'eat')
(-1, 1, 'eat')
>>> undecorated(-1, 1, 'eat')
(1, 'eat')
4
def decorated(item):
    return (-item[0],) + item

def undecorated(item):
    return item[1:]
7
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
8
def decorated(item):
    return (-item[0],) + item

def undecorated(item):
    return item[1:]
5
>>> decorated(1, 'eat')
(-1, 1, 'eat')
>>> undecorated(-1, 1, 'eat')
(1, 'eat')
8
def decorated(item):
    return (-item[0],) + item

def undecorated(item):
    return item[1:]
7
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
8
from queue import PriorityQueue

q = PriorityQueue()

q.put((2, 'code'))
q.put((3, 'eat'))
q.put((1, 'sleep'))

while not q.empty():
    next_item = q.get()
    print(next_item)

#(1, 'sleep')
#(2, 'code')
#(3, 'eat')
1
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
8
from queue import PriorityQueue

q = PriorityQueue()

q.put((2, 'code'))
q.put((3, 'eat'))
q.put((1, 'sleep'))

while not q.empty():
    next_item = q.get()
    print(next_item)

#(1, 'sleep')
#(2, 'code')
#(3, 'eat')
3
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
8
from queue import PriorityQueue

q = PriorityQueue()

q.put((2, 'code'))
q.put((3, 'eat'))
q.put((1, 'sleep'))

while not q.empty():
    next_item = q.get()
    print(next_item)

#(1, 'sleep')
#(2, 'code')
#(3, 'eat')
1
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
1
from queue import PriorityQueue

q = PriorityQueue()

q.put((2, 'code'))
q.put((3, 'eat'))
q.put((1, 'sleep'))

while not q.empty():
    next_item = q.get()
    print(next_item)

#(1, 'sleep')
#(2, 'code')
#(3, 'eat')
7
from queue import PriorityQueue

q = PriorityQueue()

q.put((2, 'code'))
q.put((3, 'eat'))
q.put((1, 'sleep'))

while not q.empty():
    next_item = q.get()
    print(next_item)

#(1, 'sleep')
#(2, 'code')
#(3, 'eat')
7 Làm thế nào để tôi có được hàng đợi ưu tiên của tôi theo thứ tự ngược lại?

Làm thế nào để tôi có được hàng đợi ưu tiên của tôi theo thứ tự ngược lại?

Bạn có thể cung cấp một đối tượng so sánh tùy chỉnh xếp hạng các phần tử theo thứ tự ngược (lhs. bằng (rhs)) return 0; return -1;}});provide a custom Comparator object that ranks elements in the reverse order: PriorityQueueprovide a custom Comparator object that ranks elements in the reverse order: PriorityQueue

pq = new PriorityQueue(defaultSize, new Comparator() { public int compare(Integer lhs, Integer rhs) { if (lhs < rhs) return +1; if (lhs. equals(rhs)) return 0; return -1; } });

Làm thế nào để bạn đảo ngược một hàng đợi ưu tiên trong Python?

Để đảo ngược thứ tự của hàng đợi ưu tiên, hãy sắp xếp hàng đợi bằng phương thức Sắp xếp () và đặt đối số ngược thành true.Theo mặc định, hàng đợi được sắp xếp theo thứ tự tăng dần.sort the queue using the sorted() method and set the reverse argument to True. By default, the queue is sorted in ascending order.sort the queue using the sorted() method and set the reverse argument to True. By default, the queue is sorted in ascending order.

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.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.

Làm thế nào để bạn tạo một danh sách ưu tiên trong Python?

Triển khai hàng đợi ưu tiên Python Để thực hiện hàng đợi ưu tiên trong Python, chúng ta phải khai báo một danh sách Python trống trong đó các yếu tố được chèn bằng phương thức append () của lớp danh sách.Danh sách sau đó được sắp xếp theo thứ tự tăng dần.Vòng lặp trong khi được sử dụng để truy xuất các phần tử bằng phương thức pop ().declare an empty Python list into which elements are inserted using the append() method of list class. The list is then sorted in ascending order. The While loop is used to retrieve the elements using the pop() method.declare an empty Python list into which elements are inserted using the append() method of list class. The list is then sorted in ascending order. The While loop is used to retrieve the elements using the pop() method.