heapq heapq module

heapq module

 

heapq --nlargest module has two functions and can find a list nsmallest, ancestral, and other data structures set maximum or minimum of n elements, and returns a list of

Copy the code
import heapq

nums = {1, 8, 9, 41, 5, 6, -5, 21, 42, 37, 2}
# nums = [1, 8, 9, 41, 5, 6, -5, 21, 42, 37, 2]
# nums = (1, 8, 9, 41, 5, 6, -5, 21, 42, 37, 2)
print(heapq.nlargest(3, nums))
print(heapq.nsmallest(3, nums))
Copy the code

result

[42, 41, 37]
[-5, 1, 2]

These two functions accept a parameter key, allowing them to work on more complex data structures

Copy the code
import heapq

portfolio = [
    {'name': 'Jan', 'age': 25, 'wages': 100},
    {'name': 'ming', 'age': 10, 'wages': 55},
    {'name': 'san', 'age': 32, 'wages': 321.5},
    {'name': 'si', 'age': 28, 'wages': 222.2},
    {'name': 'wu', 'age': 43, 'wages': 256},
]
cheap = heapq.nsmallest(3, portfolio, key=lambda s: s['wages'])
expensive = heapq.nlargest(3, portfolio, key=lambda s: s['wages'])
print(cheap)
print(expensive)
Copy the code

result

[{'age': 10, 'name': 'ming', 'wages': 55}, {'age': 25, 'name': 'Jan', 'wages': 100}, {'age': 28, 'name': 'si', 'wages': 222.2}]
[{'age': 32, 'name': 'san', 'wages': 321.5}, {'age': 43, 'name': 'wu', 'wages': 256}, {'age': 28, 'name': 'si', 'wages': 222.2}]

If looking for the maximum or minimum of n elements, and compared with the data structure element, n is small, then the performance of the above method is not so optimistic, in such a case we can use the stack

import heapq

nums = [1, 4, 5, 7, 36, -3, 65, 33, 26, 28, 2, 4]
heap = list(nums)
heapq.heapify(heap)
print(heap)

result

[-3, 2, 1, 7, 4, 4, 65, 33, 26, 28, 36, 5]

These functions will first be converted into the underlying data list, and the element will be arranged in a stack

The most important is the performance heap heap [0] is always the smallest element, the next element may in turn by heapq.heappop () method finds

The method will first element (the smallest) the pop-up and then replaced by the second element (complexity of this operation is O (logN), N represents the size of the heap)

 

heapq --nlargest module has two functions and can find a list nsmallest, ancestral, and other data structures set maximum or minimum of n elements, and returns a list of

Copy the code
import heapq

nums = {1, 8, 9, 41, 5, 6, -5, 21, 42, 37, 2}
# nums = [1, 8, 9, 41, 5, 6, -5, 21, 42, 37, 2]
# nums = (1, 8, 9, 41, 5, 6, -5, 21, 42, 37, 2)
print(heapq.nlargest(3, nums))
print(heapq.nsmallest(3, nums))
Copy the code

result

[42, 41, 37]
[-5, 1, 2]

These two functions accept a parameter key, allowing them to work on more complex data structures

Copy the code
import heapq

portfolio = [
    {'name': 'Jan', 'age': 25, 'wages': 100},
    {'name': 'ming', 'age': 10, 'wages': 55},
    {'name': 'san', 'age': 32, 'wages': 321.5},
    {'name': 'si', 'age': 28, 'wages': 222.2},
    {'name': 'wu', 'age': 43, 'wages': 256},
]
cheap = heapq.nsmallest(3, portfolio, key=lambda s: s['wages'])
expensive = heapq.nlargest(3, portfolio, key=lambda s: s['wages'])
print(cheap)
print(expensive)
Copy the code

result

[{'age': 10, 'name': 'ming', 'wages': 55}, {'age': 25, 'name': 'Jan', 'wages': 100}, {'age': 28, 'name': 'si', 'wages': 222.2}]
[{'age': 32, 'name': 'san', 'wages': 321.5}, {'age': 43, 'name': 'wu', 'wages': 256}, {'age': 28, 'name': 'si', 'wages': 222.2}]

If looking for the maximum or minimum of n elements, and compared with the data structure element, n is small, then the performance of the above method is not so optimistic, in such a case we can use the stack

import heapq

nums = [1, 4, 5, 7, 36, -3, 65, 33, 26, 28, 2, 4]
heap = list(nums)
heapq.heapify(heap)
print(heap)

result

[-3, 2, 1, 7, 4, 4, 65, 33, 26, 28, 36, 5]

These functions will first be converted into the underlying data list, and the element will be arranged in a stack

The most important is the performance heap heap [0] is always the smallest element, the next element may in turn by heapq.heappop () method finds

The method will first element (the smallest) the pop-up and then replaced by the second element (complexity of this operation is O (logN), N represents the size of the heap)

 

Guess you like

Origin www.cnblogs.com/xdlzs/p/11200393.html