Python Chapter 11 - common core module 01 collections module

Keywords: Python REST Lambda

python calls itself "Batteries included" because it provides many built-in modules that can be used without installation and configuration

This chapter mainly introduces some common built-in core modules of python

Common core modules of Python

1, collections module

This is a useful container module, which provides many useful collections to make up for the general built-in containers: list, dict, tuple, set

1.1 namedtuple()

namedtuple() is a factory function used to create a child type of tuple named dtuple

Our previous tuples can only be accessed through subscripts. When namedtuple accesses elements, it can be accessed by using something similar to attributes

Basic use

from collections import namedtuple

# Parameter 1: name of the type of tuple to be created parameter 2: list of properties owned by the new type
# It returns a class whose class name is point (determined by parameter 1) and has two properties x, y
# The variable Point just means that we have redefined the class that the variable points to
Point = namedtuple("Point", ["x", "y"])
print(Point)
print(issubclass(Point, tuple))  # It's really a subclass of tuple

# Use the returned type to create an object that represents a point in a plane
p1 = Point(x=10, y=20)
print(p1.x)
print(p1.y)

Explain:

  1. It can be easily seen from the above that using namedtuple can easily define a data type
  2. It has the immutability of tuple and can be quoted according to attributes. One word: it's cool to use
  3. Define a circle representing a plane: circle = namedtuple ('circle ', ['x','y ','r'])

Inherit from tuple

Since the class returned by namedtuple inherits from tuple, the properties and methods of tuple can be used here

For example, use subscripts to access, use for to iterate, and so on

from collections import namedtuple

Point = namedtuple("Point", ["x", "y"])
p1 = Point(x=10, y=20)

print(p1[0])
print(p1[1])

for i in p1:
    print(i)

3 new methods and 2 new attributes

Class method: make (sequence or iterator)

Create an instance from a known sequence or iterator

from collections import namedtuple

Point = namedtuple("Point", ["x", "y"])

nums = [20, 100]
p1 = Point._make(nums)
print(p1)

p2 = Point._make(range(10, 12))
print(p2)

Instance method: ()

Return a list (an ordereddict starting from 3.1f)

from collections import namedtuple

Point = namedtuple("Point", ["x", "y"])
p1 = Point(10, 20)
d = p1._asdict()
print(d)

Instance method: replace (key parameter)

Change the value of a property. Since namedtuple is immutable, a new namedtuple instance object is returned

from collections import namedtuple

Point = namedtuple("Point", ["x", "y"])
p1 = Point(10, 20)
p2 = p1._replace(y=100)
print(p2)

Class property: "source"

Return the source code of the created class

Class properties: "fields"

Returns all properties of the created class

from collections import namedtuple

Point = namedtuple("Point", ["x", "y"])
print(Point._fields)

Type 1.2: deque

deque is a two-way queue

Pronunciation: deck is short for "double ended queue"

deque thread safety, memory efficiency, support to add and delete elements at both ends

Relative list is mainly reflected in the high efficiency of adding and deleting

Creating deque objects

deque([iterable[, maxlen]])
Both parameters are optional
Parameter 1: the iteratable object will start the data in the near two terminal queue
Parameter 2: the maximum length allowed for a two terminal queue. If the number of elements exceeds this one, only the ones added later will be retained

from collections import deque

d = deque(range(10))
print(d)

Methods and properties

append(x)

Add elements to the right side of the queue
Note: for a queue, the left refers to the head of the team and the right refers to the tail of the team

appendleft(x)

Add elements to the left side of the queue

clear()

All elements in the case queue, then the length becomes 0

copy()

Elements in shallow replication queue (new in 3.5)

count(x)

Count the number of times the specified element x appears in the team

extend(iterable)

Extend queue right

extendleft(iterable)

Extend queue left

index(x)

Find the subscript x first appears in the team. If it is not found, an exception will be thrown

insert(i, x)

Insert x into the position with the subscript i

pop()

Delete and return the rightmost element

popleft

Delete and return the leftmost element

remove(x)

Delete the first x in the queue

reverse()

Flip elements in queue

Read only property: maxlen

The maximum number of allowed elements when creating a queue

Category 1.3: Counter

Counter is used to count the number of occurrences of elements in a collection

Counter is a subclass of dict, and each key value pair represents the element and the number of element occurrences

Create Counter object

Counter([iterable-or-mapping])

Parameter: iteration type or mapping type to be counted

from collections import Counter

# Create a Counter with an iterative type
c1 = Counter("abcabc3344efg")

print(c1)

# Create a Counter through dict
c2 = Counter({"a": 3, "b": 4})    # Indicates that a appears three times
print(c2)

# Create a Counter by keyword
c3 = Counter(cats=4, dogs=8)    # Indicates that cats has appeared four times
print(c3)

Some useful methods

elements()

Based on the statistics, an object of iteratable type containing all elements is returned

most_common(n)

Return the first n elements with the most occurrences

from collections import Counter

c1 = Counter("abcabc3344efg")
print(sorted(c1.elements()))    # All elements
print(c1.most_common(2))

c2 = Counter({"a": 3, "b": 4})
print(sorted(c2.elements()))
print(c2.most_common(2))

c3 = Counter(cats=4, dogs=8)
print(sorted(c3.elements()))
print(c3.most_common(1))

1.4 class: defaultdict

In the past, when we used dict, if we accessed a nonexistent key, we would throw an exception. Using defaultdict can avoid this problem

Defaultdict (function)

Explain:

  1. If the accessed key does not exist, the passed function will be called, taking the function as value
  2. The rest of the use is the same as dict
from collections import defaultdict

d = defaultdict(lambda: "Default value")
d["b"] = "bbb"
# If the key does not exist, call the function, take the return value of the function as the value, and store the key value pair in the defaultdict
print(d["a"])    
print(d["b"])
print(d)

from collections import defaultdict

s = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)]

d = defaultdict(list)
for k, v in s:
    d[k].append(v)
    

print(sorted(d.items()))

from collections import defaultdict
# Count the number of occurrences of each character
s = "abcdabAbc"

d = defaultdict(int)
for k in s:
    d[k] += 1
    

print(sorted(d.items()))

Class 1.5: OrderedDict

The key value pairs of dict are unordered

Ordered dict is a dict that can record the insertion order of key value pairs

from collections import OrderedDict

od = OrderedDict()
od["a"] = 10
od["c"] = 20
od["b"] = 40
for k, v in od.items():
    print(k + " : " + str(v))

Posted by DataSpy on Sat, 04 Apr 2020 13:47:50 -0700