In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Editor to share with you Python how to connect the list, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to know it!
1. The most intuitive addition > list01 = [1mage2, 3] > list02 = [4, 5, 5, 6] > > list03 = [7, 8, 9] > list01 + list02 + list03 [1, 2, 4, 5, 6, 7, 8] >
Use + to add multiple lists, you should understand, say no more.
two。 With itertools
Itertools has a very powerful built-in module in Python that is dedicated to manipulating iterable objects.
As described in previous articles, the itertools.chain () function is used to first concatenate iterating objects (in this case, lists) to form a larger iterable object.
Finally, you use list to turn it into a list.
> from itertools import chain > list01 = [1Perry 2, list02 3] > list02 = [4, 5, 5, 6] > list03 = [7, 8, list (list01, list02)] > > 3. Use * to unpack
On Python: seven ways to merge dictionaries refer to the use of * * unpackable dictionaries.
Similar to it, use * to unpack the list. * and * * it is common to set variable parameters when defining a function.
Now I'll take it out separately and use it in the merge of multiple lists.
Examples are as follows:
> list01 = [1 list01 2 list02 3] > list02 = [4 beers 5 legends 6] > [* list01, * list02] [1 mine2 bees 3, 4 bees 5 bees 6] > > 4. Use extend
In dictionaries, you can update in place using update, while in lists, you can use extend to expand the list itself.
> list01 = [1, list02 2, 3] > list02 = [4, 5, 5, 6] > list01.extend (list02) > list01 [1, 1, 2, 3, 4, 5, 5, 5] 5. Use list derivation
There is a very Pythonnic way to generate lists, collections, and dictionaries in Python.
That's list parsing, set parsing and dictionary parsing, which are usually favorites of Python enthusiasts, so today's topic: can list merging and list derivation still do the job?
Of course. The sample code is as follows:
> list01 = [1 list01, list02, list03) for x in l] > list02 = [4, list02, list03) for x in l] > 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 that can be used to merge multiple lists, as shown below
> list01 = [1pje 2 list02 3] > list02 = [4je 5je 6] > list03 = [7je 8pm 9] > from heapq import merge > list (list01, list02, list03)] >
Note that in addition to merging multiple lists, heapq.merge sorts the merged final list.
> list01 = [2pje 5pr 3] > list02 = [1je 4je 6] > list03 = [7je 9je 8] > from heapq import merge > list (merge (list01, list02, list03)) [1,2,4,5] >
Its effect is equivalent to the following line of code:
Sorted (itertools.chain (* iterables))
If you want a list that is always orderly, think of heapq.merge for the first time, because it uses heap sorting and is very efficient. But if you don't want an ordered list, don't use it.
7. With the help of magic
In the previous article, the magic methods are introduced very fully.
A very complete guide to Python magic methods that are easy to understand (I)
A very complete guide to Python magic methods that are easy to understand (part two)
One of the magic methods is _ _ add__,. In fact, when we use the first method list01 + list02, the interior is actually acting on the magic method _ _ add__.
So the following two methods are actually equivalent
> list01 = [1 list02 2, 5] > list02 = [4, 5, 5, 6] > list01 + list02 [1, 2, 4, 5, 6] > list01.__add__ (list02) [1, 2, 4, 5] >
To borrow this magic feature, we can reduce this method to merge multiple lists. The sample code is as follows
> list01 = [1, list01, list02, list03) > list02 = [4, 5, 5, 6] > list03 = [7, 8] > from functools import reduce > reduce (list.__add__, (list01, list02, list03))] > > 8. Use yield from
In an early article (concurrent programming 08 | in-depth understanding of yield from syntax), I introduced the meaning and usage of yield from in detail.
The yield from is followed by an iterable object that iterates over and returns each of these elements.
Therefore, we can customize a utility function to merge lists as follows.
> list01 = [1 lists 2, 3] > list02 = [4, 5, 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] > > above is all the contents of this article "how to connect Python list". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 290
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.