collections
Moduleheapq
Modulebisect
ModuleAdvanced data structures in Python are more complex and specialized than basic data types like lists, tuples, and dictionaries. They are designed to solve specific problems more efficiently. For example, the collections
module provides several advanced data structures such as Counter
, OrderedDict
, and deque
. These data structures have unique features and operations that are not available in basic data types.
deque
(double - ended queue) allows for efficient insertion and deletion at both ends, which is much faster than a regular list in some cases.Counter
to count the frequency of elements in a list is more concise than implementing the counting logic manually.collections
ModuleCounter
The Counter
is a dictionary subclass for counting hashable objects. It is used to count the frequency of elements in a collection.
from collections import Counter
# Create a Counter object
words = ['apple', 'banana', 'apple', 'cherry', 'banana', 'apple']
word_count = Counter(words)
# Print the count of each word
print(word_count)
# Output: Counter({'apple': 3, 'banana': 2, 'cherry': 1})
# Get the most common element
print(word_count.most_common(1))
# Output: [('apple', 3)]
OrderedDict
The OrderedDict
remembers the order in which items are inserted. It is useful when the order of elements matters.
from collections import OrderedDict
# Create an OrderedDict
od = OrderedDict()
od['a'] = 1
od['b'] = 2
od['c'] = 3
# Iterate over the OrderedDict
for key, value in od.items():
print(key, value)
deque
The deque
is a double - ended queue that supports efficient insertion and deletion at both ends.
from collections import deque
# Create a deque
d = deque([1, 2, 3])
# Append an element to the right
d.append(4)
print(d) # Output: deque([1, 2, 3, 4])
# Append an element to the left
d.appendleft(0)
print(d) # Output: deque([0, 1, 2, 3, 4])
# Remove an element from the right
d.pop()
print(d) # Output: deque([0, 1, 2, 3])
# Remove an element from the left
d.popleft()
print(d) # Output: deque([1, 2, 3])
heapq
ModuleThe heapq
module provides an implementation of the heap queue algorithm, also known as the priority queue algorithm.
import heapq
# Create a list
nums = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]
# Convert the list into a heap
heapq.heapify(nums)
# Push an element into the heap
heapq.heappush(nums, 0)
# Pop the smallest element from the heap
smallest = heapq.heappop(nums)
print(smallest) # Output: 0
bisect
ModuleThe bisect
module provides support for maintaining a list in sorted order without having to sort the list after each insertion.
import bisect
# Create a sorted list
sorted_list = [1, 3, 5, 7, 9]
# Find the insertion point to maintain sorted order
insert_point = bisect.bisect(sorted_list, 4)
print(insert_point) # Output: 2
# Insert an element into the sorted list
bisect.insort(sorted_list, 4)
print(sorted_list) # Output: [1, 3, 4, 5, 7, 9]
A priority queue can be implemented using the heapq
module. In a priority queue, elements with higher priority are dequeued first.
import heapq
class PriorityQueue:
def __init__(self):
self._queue = []
self._index = 0
def push(self, item, priority):
heapq.heappush(self._queue, (-priority, self._index, item))
self._index += 1
def pop(self):
return heapq.heappop(self._queue)[-1]
pq = PriorityQueue()
pq.push('task1', 1)
pq.push('task2', 3)
pq.push('task3', 2)
print(pq.pop()) # Output: 'task2'
The bisect
module can be used to perform binary search in a sorted list.
import bisect
sorted_list = [1, 3, 5, 7, 9]
def binary_search(sorted_list, target):
index = bisect.bisect_left(sorted_list, target)
if index < len(sorted_list) and sorted_list[index] == target:
return index
return -1
result = binary_search(sorted_list, 5)
print(result) # Output: 2
Counter
object.deque
instead of a regular list.bisect
for binary search instead of implementing your own binary search algorithm.The Python’s Power Tools: Advanced Data Structures Course provides a wealth of knowledge and tools for developers to handle complex data processing tasks more efficiently. By understanding the fundamental concepts, usage methods, common practices, and best practices of advanced data structures, you can write more concise, efficient, and readable code. Whether you are a beginner or an experienced Python developer, mastering these advanced data structures will definitely enhance your programming skills.