There are many (and growing) advanced features in Python that Python enthusiasts love. In the eyes of these people, to be able to write advanced features that the average developer can't understand is to be a master, to be a god.
But you have to realize that showing off is a big no-no in teamwork.
Why do you say that? I'll say what I think:
- The cleaner the code, the clearer the logic, the less likely you are to make mistakes;
- In teamwork, your code is not the only thing you maintain, and reducing the cost of reading/understanding code logic for others is a good virtue
- Simple code that will only use the most basic syntactic sugar, complex advanced features that will have more dependencies (e.g. version of the language)
This is the third installment of the "Showstopper Series", in which I'm going to summarize the showstoppers that I've seen. Here, if you're a Python enthusiast, you can learn some cool code-writing tricks. In the meantime, it might help you when you're reading other people's code.
1. Most intuitive summation
>>> list01 = [1,2,3] >>> list02 = [4,5,6] >>> list03 = [7,8,9] >>> >>> list01 + list02 + list03 [1, 2, 3, 4, 5, 6, 7, 8, 9] >>>
utilization+
Summing multiple lists, you should get it, not much more.
2. Leveraging itertools
itertools has a very powerful built-in module in Python that specializes in manipulating iterable objects.
As described in the previous article, the use of the()
Functions first iterable objects (in this case, lists) are concatenated to form a larger iterable object.
Finally, you use list to convert it to a list.
>>> from itertools import chain >>> list01 = [1,2,3] >>> list02 = [4,5,6] >>> list03 = [7,8,9] >>> >>> list(chain(list01, list02)) [1, 2, 3, 4, 5, 6, 7, 8, 9] >>>
3. Use of * unpacking
existPython Showstopper: Seven Ways to Merge DictionariesReference was made to the use of**
Unpackable dictionary.
Similar to it, the use of*
You can unpack the list.*
cap (a poem)**
Commonly used in function definitions to set variable parameters.
Now I've singled it out for use in merging multiple lists.
Examples are shown below:
>>> list01 = [1,2,3] >>> list02 = [4,5,6] >>> >>> [*list01, *list02] [1, 2, 3, 4, 5, 6] >>>
4. Using extend
In a dictionary, use update to update in place, and in a list, use extend to extend the list itself.
>>> list01 = [1,2,3] >>> list02 = [4,5,6] >>> >>> (list02) >>> list01 [1, 2, 3, 4, 5, 6]
5. Use of tabular derivatives
Python has a very Pythonnic way of writing lists, collections, and dictionaries.
That's list parsers, set parsers, and dictionary parsers, usually the favorites of Python enthusiasts, so are list derivatives still up to the task for today's topic: list merging?
Of course you can, the specific sample code is as follows:
>>> list01 = [1,2,3] >>> list02 = [4,5,6] >>> list03 = [7,8,9] >>> >>> [x for l in (list01, list02, list03) for x in l] [1, 2, 3, 4, 5, 6, 7, 8, 9] >>>
6. Use of heapq
heapq is a standard module for Python that provides an implementation of the heap sort algorithm.
The module has a merge method that can be used to merge multiple lists, as follows
>>> list01 = [1,2,3] >>> list02 = [4,5,6] >>> list03 = [7,8,9] >>> >>> from heapq import merge >>> >>> list(merge(list01, list02, list03)) [1, 2, 3, 4, 5, 6, 7, 8, 9] >>>
Note that in addition to merging multiple lists, it also sorts the final merged list.
>>> list01 = [2,5,3] >>> list02 = [1,4,6] >>> list03 = [7,9,8] >>> >>> from heapq import merge >>> >>> list(merge(list01, list02, list03)) [1, 2, 4, 5, 3, 6, 7, 9, 8] >>>
Its effect is equivalent to the following line of code:
sorted((*iterables))
If you want an always-ordered list, think of , because it uses heap sorting, which is very efficient. But if you don't want an ordered list, don't use it.
7. Recourse to magical methods
In the previous post, the magic method was described in full.
A very full guide to easy-to-understand Python magic methods (top)
A Very Complete Guide to Easy-to-Understand Python Magic Methods (below)
One of the magic methods is__add__
In fact, when we use the first method, list01 + list02, it actually works internally on the__add__
This magic method on the .
So the following two methods are actually equivalent
>>> list01 = [1,2,3] >>> list02 = [4,5,6] >>> >>> list01 + list02 [1, 2, 3, 4, 5, 6] >>> >>> >>> list01.__add__(list02) [1, 2, 3, 4, 5, 6] >>>
Borrow this magic feature, we can reduce this method to merge multiple lists, the sample code is as follows
>>> list01 = [1,2,3] >>> list02 = [4,5,6] >>> list03 = [7,8,9] >>> >>> from functools import reduce >>> reduce(list.__add__, (list01, list02, list03)) [1, 2, 3, 4, 5, 6, 7, 8, 9] >>>
8. Use of yield from
In a very early post (Concurrent Programming 08|Deeper Understanding of yield from Syntax), I covered the meaning of yield from and how to use it in great detail.
The yield from can be followed by an iterable object that iterates over and returns each of its elements.
Therefore, we can customize a tool function for merging lists like the following.
>>> list01 = [1,2,3] >>> list02 = [4,5,6] >>> list03 = [7,8,9] >>> >>> def merge(*lists): ... for l in lists: ... yield from l ... >>> list(merge(list01, list02, list03)) [1, 2, 3, 4, 5, 6, 7, 8, 9] >>>
After learning Python for so long, I didn't realize there were so many ways to merge lists. The main point of this post is not to let you master all seven ways to merge lists. In fact, you only need to choose the one that works best.
But in collaborative work, or in reading other people's code, you will inevitably come across all sorts of ways of writing, when you can subconsciously know that this is doing the operation of merging lists, then this article is meaningful.
The above is Python programming skills connected list of eight methods of operation of the details, more information about Python connected list operation please pay attention to my other related articles!