Python Advanced: simple examples of counter, orderedDict, defaultdict, deque, queue

Posted by Gmunky on Tue, 23 Nov 2021 02:43:20 +0100

Introduction: In addition to the built-in data types (int, float, str, list, tuple, dict) provided by python, the collections module also provides other data types. The collections module implements some specific data types, which can replace the built-in data types commonly used in Python, such as dict, list, set, tuple. In short, the basic data types are further processed.

Main types: This module is only introduced by deque, Counter, OrderedDict, defaultdict.

namedtuple(): Factory function for creating tuple subclasses with named fields
deque:        List container with fast attachments and pop-ups at both ends
Counter: For calculating hash objects dict Subclass
OrderedDict: dict Subclass, used to remember added order entries
defaultdict: Call factory functions to provide missing values dict Subclass
UserDict: Wrap around dictionary objects to simplify dict Subclassification
UserList: Wrap around list objects to simplify list subcategorization
UserString:  Wrapping around string objects to simplify string subclassing

Import before use:

import collections

1. Counter: Count the number of elements and return them as a dictionary, for example: {Element: Number of elements}, if the number of times is the same, then press ascii code from small to large.

Common methods in Counter:

most_common(self, n=None),
elements(self)
update(*args, **kwds)
subtract(*args, **kwds)
copy(self)

Simple examples:

import collections

data = "message"
result = collections.Counter(data)

# Number of corresponding elements and statistics elements
print(result)  # Counter({'e': 2, 's': 2, 'm': 1, 'a': 1, 'g': 1})

# most_common
# Displays are sorted by the number of elements, and all are taken unless filled in by default
print(result.most_common())

# Displays are sorted by the number of elements, taking the top three.
print(result.most_common(3))  # [('e', 2), ('s', 2), ('m', 1)]

# Elements: Iterate elements, repeating each element the same number of times as its count
print(sorted(collections.Counter("ABCABC").elements()))  # ['A', 'A', 'B', 'B', 'C', 'C']

# update: increase the number of repetitions of an element
data1 = "abc"
c1 = collections.Counter(data1)
c1.update("ab")
print(c1)  # Counter({'a': 1, 'b': 1, 'c': 1}) + Counter({'a': 1, 'b': 1}) = Counter({'a': 2, 'b': 2, 'c': 1})

# subtract: reduce element repetitions
data2 = "abc"
c2 = collections.Counter(data2)
c2.subtract("ab")
print(c2)  # Counter({'a': 1, 'b': 1, 'c': 1}) - Counter({'a': 1, 'b': 1}) = Counter({'c': 1, 'a': 0, 'b': 0})

2. OrderedDict: Ordered dictionaries are also a subclass of dictionaries. OrderedDict maintains the order in which elements are inserted during iterations. OrderedDict maintains a two-way chain table that is sorted by the order in which keys are inserted. Each time a new element is inserted, it is placed at the end of the list. Repeated assignments to an existing key do not change the order of the keys. It is important to note that an OrderedDict is twice the size of a regular dictionary because it maintains another linked list internally.

Common methods in orderedDict:

clear: Empty Dictionary
popitem: Ordered deletion, similar to a stack, deleted in a LIFO order
pop: Delete the specified key-value pair
move_to_end: Moves the specified key-value pair to the last position
setdefault: Set default value, default is None,You can also specify a value
update: Update dictionary, update if available, add if none

Simple examples:

import collections

dict1 = collections.OrderedDict({"name": "Tom", "age": 25, "address": "CN"})
print(dict1)  # OrderedDict([('name', 'tom'), ('age', 25), ('address', 'HK')])

# Ordered traversal name:Tom age:25 address:CN
for k, v in dict1.items():
    print("{k}:{v}".format(k=k, v=v), end=" ")

print()
# clear: empty the dictionary
dict1.clear()
print(dict1)  # OrderedDict()

# popitem: ordered deletion, similar to a stack, deleted in a LIFO order
dict2 = collections.OrderedDict({"name": "Jo", "age": 26, "address": "HK"})
print(dict2.popitem())  # ('address', 'HK')

# pop: delete the specified key-value pair
dict3 = collections.OrderedDict({"name": "David", "age": 27, "address": "JP"})
print(dict3.pop("age"))  # 27

# move_to_end: Moves the specified key-value pair to the last position
dict4 = collections.OrderedDict({"name": "LiLei", "age": 28, "address": "UK"})
dict4.move_to_end("name")
print(dict4)  # OrderedDict([('age', 28), ('address', 'UK'), ('name', 'LiLei')])

# setdefault: Set the default value, None by default, or specify a value
dict5 = collections.OrderedDict({"name": "HanMeiMei", "age": 29, "address": "US"})
dict5.setdefault("sex", "Man")
print(dict5["sex"])  # Man

# Update: Update the dictionary, update it if you want, add it if you don't want, consistent with the parent dictionary, no more details.

3. Default dictionary: A dictionary in which keys map multiple values and a dictionary with default values.

Simple examples:

from collections import defaultdict

d1 = defaultdict(list)
d1['a'].append([1, 2, 3])
d1['b'].append(4)
d1['c'].append(5)
print(d1)  # defaultdict(<class 'list'>, {'a': [[1, 2, 3]], 'b': [2], 'c': [3]})

d2 = defaultdict(set)
d2['a'].add(6)
d2['a'].add(7)
d2['b'].add(8)
print(d2)  # defaultdict(<class 'set'>, {'a': {6, 7}, 'b': {8}})

4. Two-way queue (deque): similar to a list, allowing elements to be manipulated on both ends

deque Common methods unique to: Other list Operations such as: pop,index,count ignore

appendleft: Add elements from left side of queue
extendleft: Expand from left side of queue
popleft: Remove values from the left side of the queue

rotate: 
Move the elements in the queue if n<0,Then move the leftmost elements of the queue to the rightmost side in turn,
Conversely, n>0,Move the rightmost element of the queue to the leftmost

Simple examples:

from collections import deque

# Create Queue
deq1 = deque("abc")
print(deq1)  # deque(['a', 'b', 'c'])

# appendleft: add elements from the left side of the queue
deq2 = deque("abc")
deq2.appendleft("d")
print(deq2)  # deque(['d', 'a', 'b', 'c'])

# extendleft: expands from the left side of the queue
deq3 = deque("abc")
deq3.extendleft(["d", "e"])
print(deq3)  # deque(['e', 'd', 'a', 'b', 'c'])

# popleft: Remove the value from the left side of the queue
deq4 = deque("abc")
deq4.popleft()
print(deq4)  # deque(['b', 'c'])


# rotate: 
# Move the elements in the queue, and if n < 0, move the leftmost elements in the queue to the rightmost side in turn.
# Conversely, n > 0 moves the rightmost element of the queue to the leftmost side

# n < 0
deq5 = deque("abcd")
deq5.rotate(-1)
print(deq5)  # deque(['b', 'c', 'd', 'a'])

# n > 0
deq6 = deque("abcd")
deq6.rotate(1)
print(deq6)  # deque(['d', 'a', 'b', 'c'])

Single queue: A two-way queue exists in the collections module, allowing operations on both ends of the element, while a single queue exists in the queue module, following the first-in-first-out principle, with only one end in and one end out.

Main common methods:

empty: Determines if the queue is empty or returns True
full: To determine if the queue is full, return True
put: Place an element in the queue
get: Take elements from the queue in turn according to FIFO principle
put_nowait: Add elements to the queue without blocking. If the queue is full, do not wait for a direct error. full)
get_nowait: Get elements from the queue without blocking, and if the queue is empty, do not wait for a direct error. empty)
qsize: Represents the length of the queue, that is, the number of elements
join: Block the calling thread until all tasks in the queue are processed, and task_done Cooperative use

Simple examples:

import queue

que = queue.Queue(2)  # Create a queue to represent the length of the queue
print(que)

print("init_full:", que.full())  # init_full: False

# Empty the queue and determine if it is empty, return True
print("init_empty:", que.empty())  # init_empty: True

que.put('A')  # Add elements to the queue
que.put('B')
# que.put('C')  # Continuing to add elements at this time can cause clogging wait

# To determine if the queue is full, return True
print("2ed_full:", que.full())  # 2ed_full: True

print(que.get())  # A
print(que.qsize())  # Represents the length of the queue, that is, the number of elements: when one is removed, only B remains, that is, length 1

WeChat Public Number: Play Test Development
Welcome to your attention and progress together, thank you!

Topics: Python chrome