Python standard library collections usage tutorial
Introduction
Python provides us with 4 basic data structures: list, tuple, dict, set, but when dealing with large amounts of data, these four data structures are obviously too simple. For example, the insertion efficiency of list as a one-way linked list will be relatively low in some situations. Sometimes we also need to maintain a An ordered dict. So at this time we have to use the collections package provided by the Python standard library. It provides a number of useful collection classes. Being proficient in these collection classes will not only allow us to make the code we write more Pythonic, but also improve How efficiently our programs run.
Usage of defaultdict
defaultdict(default_factory) adds default_factory on top of the ordinary dict (dictionary), so that the corresponding key (key) will be automatically generated when it does not exist Type value (value), the default_factory parameter can be specified as a list, Set, int and other legal types.
example1
>>> from collections import defaultdict >>> s = [('red', 1), ('blue', 2), ('red', 3), ('blue', 4), ('red', 1), ('blue', 4)]
We now have a list like the above. Although we have 6 sets of data, after careful observation, we found that we actually only have two colors. (color), but each color corresponds to multiple values. Now we want to convert this list into a dict (dictionary). The key (key) of this 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可以看作一个dict(字典),dict的value是一个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]})
example2
There are some imperfections in the above example, such as {'blue': [2, 4, 4], 'red': [1, 3, 1]} In this defaultdict, the blue color contains two 4s, and the red color contains two 1s. However, we do not want to contain duplicate elements. At this time, we can consider using defaultdict(set) to solve this problem. The difference between set (collection) and list (list) is that the same elements are not allowed to exist in set.
>>> d = defaultdict(set) >>> for k, v in s: ... d[k].add(v) ... >>> d defaultdict(<class 'set'>, {'blue': {2, 4}, 'red': {1, 3}})
example3
>>> s = 'hello world'
By using the form of defaultdict(int) we count the number of occurrences of each character in a string.
>>> 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})
Usage of OrderedDict
We know that the default dict (dictionary) is unordered, but in some cases we need to keep the dict ordered At this time, you can use OrderedDict, which is a subclass of dict, but it maintains the ordered type of dict on the basis of dict. Let's take a look at how to use it.
example1
>>> from collections import OrderedDict # 无序的dict >>> d = {'banana': 3, 'apple': 4, 'pear': 1, 'orange': 2}
This is an unordered dict (dictionary). Now we can use OrderedDict to make this dict ordered.
# 将d按照key来排序 >>> OrderedDict(sorted(d.items(), key=lambda t: t[0])) OrderedDict([('apple', 4), ('banana', 3), ('orange', 2), ('pear', 1)]) # 将d按照value来排序 >>> OrderedDict(sorted(d.items(), key=lambda t: t[1])) OrderedDict([('pear', 1), ('orange', 2), ('banana', 3), ('apple', 4)]) # 将d按照key的长度来排序 >>> OrderedDict(sorted(d.items(), key=lambda t: len(t[0]))) OrderedDict([('pear', 1), ('apple', 4), ('orange', 2), ('banana', 3)])
example2
Using the popitem(last=True) method allows us to delete the key-value in the dict in LIFO (first in, last out) order , that is, delete the last inserted key-value pair. If last=False, delete the key-value in the dict according to FIFO (first in, first out).
>>> d = {'banana': 3, 'apple': 4, 'pear': 1, 'orange': 2} # 将d按照key来排序 >>> d = OrderedDict(sorted(d.items(), key=lambda t: t[0])) >>> d OrderedDict([('apple', 4), ('banana', 3), ('orange', 2), ('pear', 1)]) # 使用popitem()方法来移除最后一个key-value对 >>> d.popitem() ('pear', 1) # 使用popitem(last=False)来移除第一个key-value对 >>> d.popitem(last=False) ('apple', 4)
example3
Use move_to_end(key, last=True) to change the key-value order of the ordered OrderedDict object. Through this method, we can insert any key-value in the ordered OrderedDict object to the beginning or end of the dictionary.
>>> d = OrderedDict.fromkeys('abcde') >>> d OrderedDict([('a', None), ('b', None), ('c', None), ('d', None), ('e', None)]) # 将key为b的key-value对移动到dict的最后 >>> d.move_to_end('b') >>> d OrderedDict([('a', None), ('c', None), ('d', None), ('e', None), ('b', None)]) >>> ''.join(d.keys()) 'acdeb' # 将key为b的key-value对移动到dict的最前面 >>> d.move_to_end('b', last=False) >>> ''.join(d.keys()) 'bacde'
The use of deque
#The advantage of list storing data is that searching for elements by index will be fast, but inserting and deleting elements is very slow. Because it is a singly linked list data structure. Deque is a two-way list for efficient implementation of insertion and deletion operations. It is suitable for queues and stacks and is thread-safe.
List only provides append and pop methods to insert/delete elements from the end of the list, but deque adds appendleft/popleft to allow us to efficiently insert/delete elements at the beginning of the element. Moreover, the algorithm complexity of using deque to add (append) or pop (pop) elements at both ends of the queue is about O(1), but for the operation of the list object to change the list length and data position, for example The complexity of pop(0) and insert(0, v) operations is as high as O(n). Since the operation of deque is basically the same as that of list, it will not be repeated here.
Use of ChainMap
ChainMap is used to combine multiple dicts (dictionaries) into a list (just a metaphor), which can be understood as merging multiple dictionaries. But it is different from update and more efficient.
>>> from collections import ChainMap >>> a = {'a': 'A', 'c': 'C'} >>> b = {'b': 'B', 'c': 'D'} >>> m = ChainMap(a, b) # 构造一个ChainMap对象 >>> m ChainMap({'a': 'A', 'c': 'C'}, {'b': 'B', 'c': 'D'}) >>> m['a'] 'A' >>> m['b'] 'B' # 将m变成一个list >>> m.maps [{'a': 'A', 'c': 'C'}, {'b': 'B', 'c': 'D'}] # 更新a中的值也会对ChainMap对象造成影响 >>> a['c'] = 'E' >>> m['c'] 'E' # 从m复制一个ChainMap对象,更新这个复制的对象并不会对m造成影响 >>> m2 = m.new_child() >>> m2['c'] = 'f' >>> m['c'] 'E' >>> a['c'] 'E' >>> m2.parents ChainMap({'a': 'A', 'c': 'C'}, {'b': 'B', 'c': 'D'})
Usage of Counter
example1
Counter is also a subclass of dict, it is An unordered container can be regarded as a counter, used to count the number of related elements.
>>> from collections import Counter >>> cnt = Counter() # 统计列表中元素出现的个数 >>> for word in ['red', 'blue', 'red', 'green', 'blue', 'blue']: ... cnt[word] += 1 ... >>> cnt Counter({'blue': 3, 'red': 2, 'green': 1}) # 统计字符串中元素出现的个数 >>> cnt = Counter() >>> for ch in 'hello': ... cnt[ch] = cnt[ch] + 1 ... >>> cnt Counter({'l': 2, 'o': 1, 'h': 1, 'e': 1})
example2
Use the elements() method to return an iterator (iterator) according to the number of occurrences of the element. The elements are returned in any order. If the count of elements is less than 1, will ignore it.
>>> 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' # 排序 >>> sorted(c.elements()) ['a', 'a', 'a', 'a', 'b', 'b']
Use most_common(n) to return a list, which contains the top n elements that appear 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)]
Usage of namedtuple
Use namedtuple(typename, field_names) to name the elements in the tuple 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
The above is the content of the collections usage tutorial of the Python standard library. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

In VS Code, you can run the program in the terminal through the following steps: Prepare the code and open the integrated terminal to ensure that the code directory is consistent with the terminal working directory. Select the run command according to the programming language (such as Python's python your_file_name.py) to check whether it runs successfully and resolve errors. Use the debugger to improve debugging efficiency.

VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.
