Python trick operation: eight ways to connect lists

There are many (and more and more) advanced features in the Python language, which are very popular among Python enthusiasts. In the eyes of these people, those who can write advanced features that ordinary developers can't understand are masters and great gods.

But you have to know that in teamwork, showing off skills is a taboo.

Why do you say that? Let me talk about my opinion:

  1. The simpler the code and the clearer the logic, the less error-prone;
  2. In teamwork, your code is not only maintained by you. It is a good moral to reduce the cost of reading/understanding the logic of the code.
  3. Simple code will only use the most basic syntactic sugar, and complex advanced features will have more dependencies (such as language versions)

This article is the third content of the " Hyun Technology Series ". In this series, I will summarize and count the amazing operations I have seen. Here, if you are a Python enthusiast, you can learn some cool coding skills. At the same time, reading these contents may help you to read other people's code.

1. The most intuitive addition

Use +multiple list are added, you should know, not much to say.

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

2. With itertools

Itertools has a very powerful built-in module in Python, which is specifically used to manipulate iterable objects.

In the previous article also introduced, using the itertools.chain()function first iterable (in this case is a list) are connected in series to form a larger iterable.

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, list03))
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>>

3. Use * to unpack

In Python xuanji operation (02): The combined dictionary seven methods mentioned use **can be unpacked dictionary.

And it is similar, using *unpacks the list. *And **used when the function is defined, provided with a variable parameter.

Now I use it separately to merge 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 the dictionary, use update to achieve in-place update, while in the list, use extend to achieve self-expansion of the list.

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

5. Use list comprehensions

There is a very Pythonnic way to generate lists, sets, and dictionaries in Python.

Those are list comprehensions, set comprehensions and dictionary comprehensions, which are usually the favorites of Python enthusiasts. So today's topic: list merging, can list comprehensions still be competent?

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

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

There is a 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]
>>> 

It should be noted that besides merging multiple lists, heapq.merge will also sort 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(itertools.chain(*iterables))

If you want to get a list that is always ordered, please think of heapq.merge for the first time, because it uses heap sorting, which is very efficient. But if you don't want to get a sorted list, don't use it.

7. With the help of magic methods

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

A very complete guide to easy-to-understand Python magic methods (on)

A very comprehensive guide to easy-to-understand Python magic methods (part 2)

There is a magic method is __add__, in fact, when we use the first method list01 + list02 internal actually acting __add__on 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]
>>> 

Borrowing 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 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.

After yield from, an iterable object can be followed to iterate and return each element.

Therefore, we can customize a tool function for a merged list 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]
>>>

I recommend my original " PyCharm Chinese Guide " e-book, which contains a large number (300) of illustrations . It is well-made and worthy of a collection by every Python engineer.

The address is: http://pycharm.iswbm.com

Guess you like

Origin blog.csdn.net/weixin_36338224/article/details/109037074