Hướng dẫn what does heapq mean in python? - heapq có nghĩa là gì trong python?

Cấu trúc dữ liệu HEAP chủ yếu được sử dụng để thể hiện hàng đợi ưu tiên. Trong Python, nó có sẵn bằng cách sử dụng mô -đun Feap Feapq. Thuộc tính của cấu trúc dữ liệu này trong Python là mỗi lần phần tử heap nhỏ nhất được bật ra [min-heap]. Bất cứ khi nào các yếu tố được đẩy hoặc bật lên, cấu trúc heap được duy trì. Phần tử Heap [0] cũng trả về phần tử nhỏ nhất mỗi lần. Hãy cùng xem các hoạt động khác nhau trên đống trong Python.

Tạo một đống đơn giản

Các heapify [có thể lặp lại]:- Hàm này được sử dụng để chuyển đổi có thể lặp lại thành cấu trúc dữ liệu heap. tức là theo thứ tự đống.heapify[iterable]:- This function is used to convert the iterable into a heap data structure. i.e. in heap order.

Python3

import heapq

li =

The created heap is : [1, 3, 9, 7, 5]
The modified heap after push is : [1, 3, 4, 7, 5, 9]
The popped and smallest element is : 1
9

The popped item using heappushpop[] is : 1
The popped item using heapreplace[] is : 3
0
The popped item using heappushpop[] is : 1
The popped item using heapreplace[] is : 3
1
The popped item using heappushpop[] is : 1
The popped item using heapreplace[] is : 3
2223
The popped item using heappushpop[] is : 1
The popped item using heapreplace[] is : 3
4
The popped item using heappushpop[] is : 1
The popped item using heapreplace[] is : 3
5

Output:

The created heap is :  [1, 3, 9, 7, 5]

& nbsp; các mặt hàng nối tiếp và popping một cách hiệu quả

  • Heappush [Heap, Ele]: Hàm này được sử dụng để chèn phần tử được đề cập trong các đối số của nó thành một đống. Bộ điều chỉnh được điều chỉnh, để cấu trúc heap được duy trì.: This function is used to insert the element mentioned in its arguments into a heap. Theorder is adjusted, so that heap structure is maintained.
  • Heppop [Heap]: Hàm này được sử dụng để loại bỏ và trả lại phần tử nhỏ nhất từ ​​đống. Thứ tự được điều chỉnh, để cấu trúc heap được duy trì.: This function is used to remove and return the smallest element from the heap. The order is adjusted, so that heap structure is maintained.

Python3

import heapq

li =

The created heap is : [1, 3, 9, 7, 5]
The modified heap after push is : [1, 3, 4, 7, 5, 9]
The popped and smallest element is : 1
9

The popped item using heappushpop[] is : 1
The popped item using heapreplace[] is : 3
0
The popped item using heappushpop[] is : 1
The popped item using heapreplace[] is : 3
1
The popped item using heappushpop[] is : 1
The popped item using heapreplace[] is : 3
2import5=import7

The popped item using heappushpop[] is : 1
The popped item using heapreplace[] is : 3
0
The popped item using heappushpop[] is : 1
The popped item using heapreplace[] is : 3
1
The popped item using heappushpop[] is : 1
The popped item using heapreplace[] is : 3
4heapq1

heapq2heapq3heapq4

The popped item using heappushpop[] is : 1
The popped item using heapreplace[] is : 3
0
The popped item using heappushpop[] is : 1
The popped item using heapreplace[] is : 3
1heapq7import5=import7

The popped item using heappushpop[] is : 1
The popped item using heapreplace[] is : 3
0
The popped item using heappushpop[] is : 1
The popped item using heapreplace[] is : 3
1
The popped item using heappushpop[] is : 1
The popped item using heapreplace[] is : 3
4heapq1

The created heap is : [1, 3, 9, 7, 5]
The modified heap after push is : [1, 3, 4, 7, 5, 9]
The popped and smallest element is : 1
9

The popped item using heappushpop[] is : 1
The popped item using heapreplace[] is : 3
0=2

The popped item using heappushpop[] is : 1
The popped item using heapreplace[] is : 3
0
The popped item using heappushpop[] is : 1
The popped item using heapreplace[] is : 3
1
The popped item using heappushpop[] is : 1
The popped item using heapreplace[] is : 3
2223
The popped item using heappushpop[] is : 1
The popped item using heapreplace[] is : 3
4
The popped item using heappushpop[] is : 1
The popped item using heapreplace[] is : 3
5

The created heap is : [1, 3, 9, 7, 5]
The modified heap after push is : [1, 3, 4, 7, 5, 9]
The popped and smallest element is : 1

& nbsp; các mặt hàng nối tiếp và popping một cách hiệu quảsimultaneously

  • Heappush [Heap, Ele]: Hàm này được sử dụng để chèn phần tử được đề cập trong các đối số của nó thành một đống. Bộ điều chỉnh được điều chỉnh, để cấu trúc heap được duy trì.:- This function combines the functioning of both push and pop operations in one statement, increasing efficiency. Heap order is maintained after this operation.
  • Heppop [Heap]: Hàm này được sử dụng để loại bỏ và trả lại phần tử nhỏ nhất từ ​​đống. Thứ tự được điều chỉnh, để cấu trúc heap được duy trì.:- This function also inserts and pops elements in one statement, but it is different from the above function. In this, the element is first popped, then the element is pushed. i.e, the value larger than the pushed value can be returned. heapreplace[] returns the smallest value originally in the heap regardless of the pushed element as opposed to heappushpop[].

Python3

import heapq

li =

The created heap is : [1, 3, 9, 7, 5]
The modified heap after push is : [1, 3, 4, 7, 5, 9]
The popped and smallest element is : 1
9

The created heap is : [1, 3, 9, 7, 5]
The modified heap after push is : [1, 3, 4, 7, 5, 9]
The popped and smallest element is : 1
01

The created heap is : [1, 3, 9, 7, 5]
The modified heap after push is : [1, 3, 4, 7, 5, 9]
The popped and smallest element is : 1
02

The popped item using heappushpop[] is : 1
The popped item using heapreplace[] is : 3
0
The popped item using heappushpop[] is : 1
The popped item using heapreplace[] is : 3
1
The created heap is : [1, 3, 9, 7, 5]
The modified heap after push is : [1, 3, 4, 7, 5, 9]
The popped and smallest element is : 1
05import5=import7

The popped item using heappushpop[] is : 1
The popped item using heapreplace[] is : 3
0
The created heap is : [1, 3, 9, 7, 5]
The modified heap after push is : [1, 3, 4, 7, 5, 9]
The popped and smallest element is : 1
10
The created heap is : [1, 3, 9, 7, 5]
The modified heap after push is : [1, 3, 4, 7, 5, 9]
The popped and smallest element is : 1
11
The created heap is : [1, 3, 9, 7, 5]
The modified heap after push is : [1, 3, 4, 7, 5, 9]
The popped and smallest element is : 1
12

The popped item using heappushpop[] is : 1
The popped item using heapreplace[] is : 3
0
The popped item using heappushpop[] is : 1
The popped item using heapreplace[] is : 3
1
The created heap is : [1, 3, 9, 7, 5]
The modified heap after push is : [1, 3, 4, 7, 5, 9]
The popped and smallest element is : 1
15import5=import7

The popped item using heappushpop[] is : 1
The popped item using heapreplace[] is : 3
0
The created heap is : [1, 3, 9, 7, 5]
The modified heap after push is : [1, 3, 4, 7, 5, 9]
The popped and smallest element is : 1
20
The created heap is : [1, 3, 9, 7, 5]
The modified heap after push is : [1, 3, 4, 7, 5, 9]
The popped and smallest element is : 1
11
The created heap is : [1, 3, 9, 7, 5]
The modified heap after push is : [1, 3, 4, 7, 5, 9]
The popped and smallest element is : 1
12

Output:

The popped item using heappushpop[] is : 1
The popped item using heapreplace[] is : 3

The popped item using heappushpop[] is : 1
The popped item using heapreplace[] is : 3
0
The popped item using heappushpop[] is : 1
The popped item using heapreplace[] is : 3
1
The popped item using heappushpop[] is : 1
The popped item using heapreplace[] is : 3
2223
The popped item using heappushpop[] is : 1
The popped item using heapreplace[] is : 3
4
The popped item using heappushpop[] is : 1
The popped item using heapreplace[] is : 3
5

  • & nbsp; các mặt hàng nối tiếp và popping một cách hiệu quả: This function is used to return the k largest elements from the iterable specified and satisfy the key if mentioned.
  • Heappush [Heap, Ele]: Hàm này được sử dụng để chèn phần tử được đề cập trong các đối số của nó thành một đống. Bộ điều chỉnh được điều chỉnh, để cấu trúc heap được duy trì.: This function is used to return the k smallest elements from the iterable specified and satisfy the key if mentioned.

Python3

import heapq

li =

The created heap is : [1, 3, 9, 7, 5]
The modified heap after push is : [1, 3, 4, 7, 5, 9]
The popped and smallest element is : 1
01

The popped item using heappushpop[] is : 1
The popped item using heapreplace[] is : 3
0
The popped item using heappushpop[] is : 1
The popped item using heapreplace[] is : 3
1
The created heap is : [1, 3, 9, 7, 5]
The modified heap after push is : [1, 3, 4, 7, 5, 9]
The popped and smallest element is : 1
49import5=import7

The popped item using heappushpop[] is : 1
The popped item using heapreplace[] is : 3
0
The created heap is : [1, 3, 9, 7, 5]
The modified heap after push is : [1, 3, 4, 7, 5, 9]
The popped and smallest element is : 1
54
The created heap is : [1, 3, 9, 7, 5]
The modified heap after push is : [1, 3, 4, 7, 5, 9]
The popped and smallest element is : 1
7
The created heap is : [1, 3, 9, 7, 5]
The modified heap after push is : [1, 3, 4, 7, 5, 9]
The popped and smallest element is : 1
56

The popped item using heappushpop[] is : 1
The popped item using heapreplace[] is : 3
0
The popped item using heappushpop[] is : 1
The popped item using heapreplace[] is : 3
1
The created heap is : [1, 3, 9, 7, 5]
The modified heap after push is : [1, 3, 4, 7, 5, 9]
The popped and smallest element is : 1
59import5=import7

The popped item using heappushpop[] is : 1
The popped item using heapreplace[] is : 3
0
The created heap is : [1, 3, 9, 7, 5]
The modified heap after push is : [1, 3, 4, 7, 5, 9]
The popped and smallest element is : 1
64
The created heap is : [1, 3, 9, 7, 5]
The modified heap after push is : [1, 3, 4, 7, 5, 9]
The popped and smallest element is : 1
7
The created heap is : [1, 3, 9, 7, 5]
The modified heap after push is : [1, 3, 4, 7, 5, 9]
The popped and smallest element is : 1
56

The popped item using heappushpop[] is : 1
The popped item using heapreplace[] is : 3
0
The popped item using heappushpop[] is : 1
The popped item using heapreplace[] is : 3
1
The popped item using heappushpop[] is : 1
The popped item using heapreplace[] is : 3
2223
The popped item using heappushpop[] is : 1
The popped item using heapreplace[] is : 3
4
The popped item using heappushpop[] is : 1
The popped item using heapreplace[] is : 3
5

The 3 largest numbers in list are : [10, 9, 8]
The 3 smallest numbers in list are : [1, 3, 4]

Nhập khẩu Heapq làm gì trong Python?

Mô -đun Python Heapq là một phần của thư viện tiêu chuẩn. Nó thực hiện tất cả các hoạt động Heap cấp thấp cũng như một số cách sử dụng phổ biến cấp cao cho đống. Hàng đợi ưu tiên là một công cụ mạnh mẽ có thể giải quyết các vấn đề khác nhau như viết một trình lập lịch email, tìm đường dẫn ngắn nhất trên bản đồ hoặc hợp nhất các tệp nhật ký.implements all the low-level heap operations as well as some high-level common uses for heaps. A priority queue is a powerful tool that can solve problems as varied as writing an email scheduler, finding the shortest path on a map, or merging log files.

Tại sao Heapq được sử dụng?

Hàng đợi Heap là một cấu trúc cây đặc biệt trong đó mỗi nút cha nhỏ hơn hoặc bằng nút con của nó.Trong Python, nó được triển khai bằng mô -đun FEAPQ.Nó rất hữu ích là triển khai hàng đợi ưu tiên trong đó mục hàng đợi có trọng lượng cao hơn được ưu tiên hơn trong xử lý.implementing priority queues where the queue item with higher weight is given more priority in processing.

HEAPQ tối đa hay tối đa?

8 Cấu trúc dữ liệu phổ biến Mỗi lập trình viên phải biết mô -đun HEAPQ của Python thực hiện thuật toán Hàng đợi HEAP.Nó sử dụng các đống tối thiểu trong đó chìa khóa của cha mẹ nhỏ hơn hoặc bằng với con của nó.min heap where the key of the parent is less than or equal to those of its children.

HEAPQ có nhanh hơn hàng đợi ưu tiên không?

Kết luận: Rõ ràng từ thời điểm hồ sơ rằng, Heapq chạy nhanh hơn hàm ưu tiên.Và điều này là hiển nhiên bởi vì ưu tiên sử dụng mô -đun luồng để thực hiện cấu trúc mutex để an toàn luồng trong khi thao tác các mục trong hàng đợi.heapq runs faster than PriorityQueue function. And this is obvious because PriorityQueue uses the threading module to implement a mutex structure for thread safety while manipulating items in the queue.

Bài Viết Liên Quan

Chủ Đề