27. Heapq

Section 27.1: Largest and smallest items in a collection

To find the largest items in a collection, heapq module has a function called nlargest, we pass it two arguments, the first one is the number of items that we want to retrieve, the second one is the collection name:

import heapq

numbers [1421002050322001508]

print(heapq.nlargest(4numbers)) # [200, 150, 100, 50]

Similarly, to find the smallest items in a collection, we use nsmallest function:

print(heapq.nsmallest(4numbers)) # [1, 2, 4, 8]

Both nlargest and nsmallest functions take an optional argument (key parameter) for complicated data structures. The following example shows the use of age property to retrieve the oldest and the youngest people from people dictionary:

people [

{‘firstname’‘John’‘lastname’‘Doe’‘age’30},

{‘firstname’‘Jane’‘lastname’‘Doe’‘age’25},

{‘firstname’‘Janie’‘lastname’‘Doe’‘age’10},

{‘firstname’‘Jane’‘lastname’‘Roe’‘age’22},

{‘firstname’‘Johnny’‘lastname’‘Doe’‘age’12},

{‘firstname’‘John’‘lastname’‘Roe’‘age’45}

]

oldest heapq.nlargest(2peoplekey=lambda s: s[‘age’]) print(oldest)

#Output: [{‘firstname’: ‘John’, ‘age’: 45, ‘lastname’: ‘Roe’}, {‘firstname’: ‘John’, ‘age’: 30, ‘lastname’: ‘Doe’}]

youngest heapq.nsmallest(2peoplekey=lambda s: s[‘age’]) print(youngest)

#Output: [{‘firstname’: ‘Janie’, ‘age’: 10, ‘lastname’: ‘Doe’}, {‘firstname’: ‘Johnny’, ‘age’: 12, ‘lastname’: ‘Doe’}]

Section 27.2: Smallest item in a collection

The most interesting property of a heap is that its smallest element is always the first element: heap[0]

import heapq

numbers [10421002050322001508]

heapq.heapify(numbers) print(numbers)

# Output: [2, 4, 10, 100, 8, 50, 32, 200, 150, 20]

heapq.heappop(numbers) # 2 print(numbers)

# Output: [4, 8, 10, 100, 20, 50, 32, 200, 150]

heapq.heappop(numbers) # 4 print(numbers)

# Output: [8, 20, 10, 100, 150, 50, 32, 200]

 

 

 

 

 

*This content is compiled from Stack Overflow Documentation, and the content is written by the beautiful people at Stack Overflow. 

  *This content is compiled from Stack Overflow Documentation, and the content is written by the beautiful people at Stack Overflow.  This work is licensed under cc by-sa.