Python Programming Skills Eight Operation Methods of Connection List

  • 2021-12-11 08:09:50
  • OfStack

Directory 1. The most intuitive addition 2. With itertools 3. Use * unpack 4. Use extend 5. Use list derivation 6. Use heapq 8. Use yield from

There are many (and increasingly) advanced features in the Python language that Python enthusiasts like very much. In the eyes of these people, to be able to write those advanced features that developers can't understand is a master and a great god.

But you should know that in teamwork, showing off skills is a big taboo.

Why do you say that? Let me say my own opinion:

The simpler the code and the clearer the logic, the less likely it is to make mistakes; In teamwork, your code is not only maintained by you, but it is a good character to reduce the cost of others reading/understanding the code logic Simple code, which only uses the most basic syntax sugar, complex high-level features, will have more dependencies (such as language versions)

This is the third article in the series of "Show off Skills". In this series, I will summarize and take stock of the show-off operations I have seen. Here, if you are an Python enthusiast, you can learn some writing skills for writing cool code. At the same time, reading these contents may be helpful when you read other people's code.

1. The most intuitive addition


>>> list01 = [1,2,3]
>>> list02 = [4,5,6]
>>> list03 = [7,8,9]
>>>
>>> list01 + list02 + list03
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> 

Use + Add up multiple lists, you should understand, not say much.

2. With the help of itertools

itertools has a very powerful built-in module in Python dedicated to manipulating iterative objects.

As described in the previous article, using the itertools.chain() Functions first concatenate iterable objects (in this case, lists) to form a larger iterable object.

Finally, you use list to convert it into 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. Unpack with *

In Python Hyundai Operation: 7 Ways to Merge Dictionaries Mentioned Using ** Unwrappable dictionary.

Similar to it, use * Can unpack the list. * And ** Variable parameters are often set when defining functions.

Now I take it out separately for merging multiple lists.

Examples are as follows:


>>> list01 = [1,2,3]
>>> list02 = [4,5,6]
>>>
>>> [*list01, *list02]
[1, 2, 3, 4, 5, 6]
>>>

4. Use extend

In dictionaries, update enables in-situ updating, while in lists, extend enables self-extension of lists.


>>> list01 = [1,2,3]
>>> list02 = [4,5,6]
>>>
>>> list01.extend(list02)
>>> list01
[1, 2, 3, 4, 5, 6]

5. Use a list derivation

In Python, there is a set of very Pythonnic writing methods for generating lists, sets and dictionaries.

That is list parsing, set parsing and dictionary parsing, which are usually the favorites of Python enthusiasts. So today's topic: Can list merging and list derivation be competent?

Of course, the specific example 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 heapq

heapq is a standard module of Python, which provides the implementation of heap sorting algorithm.

There is an merge method in this module, which can be used to merge multiple lists, as shown below


>>> 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 heapq. merge not only merges multiple lists, but also sorts the final list after merging.


>>> 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(itertools.chain(*iterables))

If you want a list that is always ordered, think of heapq. merge in time 1, because it uses heap sorting and is very efficient. But if you don't want to get an ordered list, don't use it.

7. Use magic methods

In the previous article, the magic method was introduced completely.

Very complete and easy-to-understand Python magic method guide (I)

Very complete and easy-to-understand Python magic method guide (below)

One of the magic methods is __add__ In fact, when we use the first method list01 + list02, the interior actually acts on the __add__ This magic method.

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]
>>> 

Using this magic feature, we can combine multiple lists with reduce. The example 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. Using yield from

In a very early article (concurrent programming 08 | in-depth understanding of yield from syntax), I introduced the meaning and usage of yield from in detail.

yield from can be followed by an iterative object that iterates and returns every element in it.

Therefore, we can customize a tool function for merging lists as follows.


>>> 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]
>>>
0

See here, there is no rising posture, after learning Python for so long, I didn't expect there are so many ways to merge the list. The main idea of this article is not to let you master all the seven ways to merge lists. In fact, you only need to choose the most convenient way.

However, when you work together or read other people's code, you will inevitably encounter various writing methods. At this time, you can subconsciously know that you are doing the operation of merging lists, so this article is meaningful.

The above is the Python programming skills link list of 8 methods of operation details, more about Python link list operation information please pay attention to other related articles on this site!


Related articles: