Python There is a lot in the language ( And more and more ) Advanced features of , yes Python Fans love . In the eyes of these people , Be able to write advanced features that the average developer can't understand , It's a master , It's the great God .
But you need to know , In teamwork , Show off your skills is taboo .
Why do you say that ? I'll tell you what I think :
The more concise the code , The clearer the logic , The less likely it is to make mistakes ;
In teamwork , You're not the only one maintaining your code , Reduce the reading of others / Understanding the cost of code logic is a good virtue
Simple code , Only use the most basic grammar sugar , Complex advanced features , There will be more dependence ( Like the language version )
This article is 「 Dazzle technology series 」 The third part of , In this series , I'll summarize and take stock of , I've seen all those fancy operations . ad locum , If you are Python enthusiasts , You can learn some cool code writing skills . meanwhile , After reading these contents , When you're reading someone else's code , Maybe it will help .
>>> list01 = [1,2,3]
>>> list02 = [4,5,6]
>>> list03 = [7,8,9]
>>>
>>> list01 + list02 + list03
[1, 2, 3, 4, 5, 6, 7, 8, 9]
In the previous article, I also introduced , Use itertools.chain() The function iterates over the object first ( This is the list ) In series , Make up a larger iteratable object .
Finally, you can use list Convert it to 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]
Similar to it , Use * Can unpack list . * and ** Often used in function definition , Set variable parameters .
Now I'm going to take it out and use it for merging multiple lists .
Examples are as follows :
>>> list01 = [1,2,3]
>>> list02 = [4,5,6]
>>>
>>> [*list01, *list02]
[1, 2, 3, 4, 5, 6]
>>> list01 = [1,2,3]
>>> list02 = [4,5,6]
>>>
>>> list01.extend(list02)
>>> list01
[1, 2, 3, 4, 5, 6]
That's list parsing , Set parsing and dictionary parsing , Usually Python The enthusiast's favorite , So today's theme : List merge , Can list derivation be competent ?
Certainly. , 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]
There is one in this module merge Method , 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 ,heapq.merge In addition to merging multiple lists , It will also sort the combined final 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's always in order , Please think of heapq.merge, Because it uses heap sort , Very efficient . But if you don't want to get an ordered list , Don't use it .
Very easy to understand Python Magic method guide ( On )
Very easy to understand Python Magic method guide ( Next )
One of the magic methods is add, actual When we use the first method list01 + list02 When , The inside is actually working on add This magic method is .
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 is used 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]
stay yield from Then you can take an iterative object , Used to iterate and return each of these elements .
therefore , We can customize a tool function to merge lists as follows .
>>> 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]
Recommend my original 《PyCharm Chinese guide 》 e-book , It contains a lot of (300 Zhang ) Diagram , Well made , It's worth every Python The engineer ordered a collection .
The address is :http://pycharm.iswbm.com
————————————————
Copyright notice : This paper is about CSDN Blogger 「 Mingo who wrote the code 」 The original article of , follow CC 4.0 BY-SA Copyright agreement , For reprint, please attach the original source link and this statement .
Link to the original text :https://blog.csdn.net/weixin_36338224/article/details/109035121