collections of python standard library

Introduction

python provides us with five basic data structures: list, tuple, dict, set, string;

Sometimes we need to maintain an orderly dict. So at this time, we will use the collection package provided by Python standard library. It provides many useful collection classes. Mastering these collection classes skillfully can not only make our code pythonic, but also improve the efficiency of our program.

defaultdict:

Default dict_factory adds default_factory to ordinary Dict (dictionary) so that when key (key) does not exist, it automatically generates the corresponding type of value (value). The default_factory parameter can be specified as list, set, int and other legal types.

>>> from collections import defaultdict
>>> s = [('red', 1), ('blue', 2), ('red', 3), ('blue', 4), ('red', 1), ('blue', 4)]

Example 1: Set default_factory to list

We now have the list above. Although we have six sets of data, when we look closely, we find that we only have two colors, but each color corresponds to more than one value. Now we want to convert this list into a Dict (dictionary), the key (key) of the dict corresponds to a color, and the value (value) of the dict is set to a list to store multiple values corresponding to the color. We can use defaultdict(list) to solve this problem.

# d can be seen as a Dict (dictionary), and the value of dict is a list (list).
>>> d = defaultdict(list)
>>> for k,v in s:
...     d[k].append(v)
...
>>> d
defaultdict(<class 'list'>, {'blue': [2, 4, 4], 'red': [1, 3, 1]})

Example 2: Set default_factory to set

There are some imperfections in the example above, such as {blue': [2, 4, 4], `red': [1, 3, 1]} which contains two blue colors and two red colors, but we don't want to contain repetitive elements, so we can consider using defaultdict(set) to solve this problem. The difference between set and list is that the same elements are not allowed in set.

>>> d = defaultdict(set)
>>> for k,v in s:
...     d[k].add(v)
...
>>> d
defaultdict(<class 'set'>, {'blue': {2, 4}, 'red': {1, 3}})

Example 3: Set default_factory to int

By using defaultdict(int), we count the number of occurrences of each character in a string.

>>> s = 'hello world'
>>> d = defaultdict(int)
>>> for k in s:
...     d[k] += 1
...
>>> d
defaultdict(<class 'int'>, {'o': 2, 'h': 1, 'w': 1, 'l': 3, ' ': 1, 'd': 1, 'e': 1, 'r': 1})

OrderedDict:

We know that the default Dict (dictionary) is out of order, but in some cases we need to keep Dict orderly. OrderedDict can be used at this time. It is a subclass (subclass) of dict, but on the basis of dict, it keeps Dict orderly. Let's see how to use it.

>>> from collections import OrderedDict
# Disordered dict
>>> d = {'banana': 3, 'apple': 4, 'pear': 1, 'orange': 2}

Example 1: This is a disordered dict. Now we can use OrderedDict to make this Dict orderly.

# Sort d by key
>>> OrderedDict(sorted(d.items(), key=lambda t: t[0]))
OrderedDict([('apple', 4), ('banana', 3), ('orange', 2), ('pear', 1)])
# Sort d by value
>>> OrderedDict(sorted(d.items(), key=lambda t: t[1]))
OrderedDict([('pear', 1), ('orange', 2), ('banana', 3), ('apple', 4)])
# Sort d by key length
>>> OrderedDict(sorted(d.items(), key=lambda t: len(t[0])))
OrderedDict([('pear', 1), ('apple', 4), ('orange', 2), ('banana', 3)])

Example 2: Using popitem(last=True) method, we can delete key-value in dict in LIFO order, that is, delete the last inserted key-value pair. If last=False, delete key-value in dict according to FIFO (first in first out).

>>> d = {'banana': 3, 'apple': 4, 'pear': 1, 'orange': 2}
# Sort d by key
>>> d = OrderedDict(sorted(d.items(), key=lambda t: t[0]))
>>> d
OrderedDict([('apple', 4), ('banana', 3), ('orange', 2), ('pear', 1)])
# Remove the last key-value pair using the popitem() method
>>> d.popitem()
('pear', 1)
# Use popitem(last=False) to remove the first key-value pair
>>> d.popitem(last=False)
('apple', 4)

Example 3: Using move_to_end(key, last=True) to change the key-value order of ordered OrderedDict objects, we can insert any key-value in the ordered OrderedDict object into the beginning or end of the dictionary.

>>> d = OrderedDict.fromkeys('abcde')
>>> d
OrderedDict([('a', None), ('b', None), ('c', None), ('d', None), ('e', None)])
# Move the key-value pair whose key is b to the end of dict
>>> d.move_to_end('b')
>>> d
OrderedDict([('a', None), ('c', None), ('d', None), ('e', None), ('b', None)])
>>> ''.join(d.keys())
'acdeb'
# Move the key-value pair whose key is b to the front of dict
>>> d.move_to_end('b', last=False)
>>> ''.join(d.keys())
'bacde'

Counter:

Example 1: Counter is also a subclass of dict. It is an unordered container. It can be seen as a counter to count the number of relevant elements.

>>> from collections import Counter
>>> cnt = Counter()
# Number of elements in a statistical list
>>> for word in ['red', 'blue', 'red', 'green', 'blue', 'blue']:
...  cnt[word] += 1
...
>>> cnt
Counter({'blue': 3, 'red': 2, 'green': 1})
# Statistics of the number of elements in a string
>>> cnt = Counter()
>>> for ch in 'hello':
...     cnt[ch] = cnt[ch] + 1
...
>>> cnt
Counter({'l': 2, 'o': 1, 'h': 1, 'e': 1})

Example 2: Use the element () method to return an iterator (iterator) according to the number of occurrences of the element. The element is returned in any order. If the number of elements is less than 1, it will be ignored.

>>> c = Counter(a=4, b=2, c=0, d=-2)
>>> c
Counter({'a': 4, 'b': 2, 'c': 0, 'd': -2})
>>> c.elements()
<itertools.chain object at 0x7fb0a069ccf8>
>>> next(c)
'a'
# sort
>>> sorted(c.elements())
['a', 'a', 'a', 'a', 'b', 'b']

Example 3: Use most_common(n) to return a list containing up to n elements in the Counter object.

>>> c = Counter('abracadabra')
>>> c
Counter({'a': 5, 'b': 2, 'r': 2, 'd': 1, 'c': 1})
>>> c.most_common(3)
[('a', 5), ('b', 2), ('r', 2)]

namedtuple:

Name the elements in the tuple with name dtuple (typename, field_names) to make the program more readable.

>>> from collections import namedtuple
>>> Point = namedtuple('PointExtension', ['x', 'y'])
>>> p = Point(1, 2)
>>> p.__class__.__name__
'PointExtension'
>>> p.x
1
>>> p.y
2


Keywords: Lambda Python less

Added by Jaxeed on Fri, 05 Jul 2019 22:08:39 +0300