collections
Module isPython
A module in the standard library that provides some useful data structures for extending built-in data types.
collections
The module contains the following important data structures:
-
OrderedDict
: Ordered dictionary, saving elements in the order of insertion. The dictionary can be traversed in the order it was added. -
defaultdict
: Default dictionary. When accessing non-existent keys, a default value will be returned and will not be thrown.KeyError
abnormal. -
Counter
: Counter, used to count the frequency of hashable objects. You can quickly calculate the number of occurrences of each element in a sequence. -
deque
: Double-ended queues, which can efficiently add and delete elements at both ends. It is faster than the list operation and supports thread-safe operation. -
namedtuple
: Named tuples, you can create a tuple class with named fields. Instead of using indexes, you can access elements of a tuple by field names.
These data structures are very useful in different scenarios and can provide more efficient data operation and more convenient data access. The appropriate data structure can be selected according to specific needs to improve the performance and readability of the code.
Sample code
Counter
(counter)Counter
A class is a simple counter that can be used to count the number of occurrences of a hashable object.
from collections import Counter words = ['apple', 'banana', 'apple', 'orange', 'banana', 'apple'] word_count = Counter(words) print(word_count) # Output: Counter({'apple': 3, 'banana': 2, 'orange': 1})print(word_count['apple']) # Output: 3print(word_count['banana']) # Output: 2print(word_count['grape']) # Output:0
defaultdict
(Default dictionary)defaultdict
A class is a dictionary that provides a default value that returns the specified default value when accessing a non-existent key.
from collections import defaultdict fruit_dict = defaultdict(int) fruit_dict['apple'] += 1 fruit_dict['banana'] += 2 print(fruit_dict) # Output: defaultdict(<class 'int'>, {'apple': 1, 'banana': 2})print(fruit_dict['apple']) # Output: 1print(fruit_dict['orange']) # Output:0(intDefault value of type)
deque
(Double-ended queue)deque
A class is a double-ended queue that supports operations from both ends of the queue.
from collections import deque queue = deque(['apple', 'banana', 'orange']) ('grape') # Add an element at the end of the queue('melon') # Add elements at the head of the queueprint(queue) # Output: deque(['melon', 'apple', 'banana', 'orange', 'grape'])() # Remove the element at the end of the queue() # Remove the element of the queue headerprint(queue) # Output:deque(['apple', 'banana', 'orange'])
This is justcollections
Several classes in the module and their examples, and some other classes and methods can be further explored and used when needed.
This is the end of this article about the detailed explanation of the python collections module. For more related content of the python collections module, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!