程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Eight methods of Python connection list

編輯:Python

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 .

  1. The most intuitive addition
    Use + Add multiple lists , You should know , Not much said .
>>> list01 = [1,2,3]
>>> list02 = [4,5,6]
>>> list03 = [7,8,9]
>>>
>>> list01 + list02 + list03
[1, 2, 3, 4, 5, 6, 7, 8, 9]
  1. With the help of itertools
    itertools stay Python There is a very powerful built-in module , It's designed to operate on iteratable objects .

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]
  1. Use * Unpack
    stay Python Dazzling operation (02): Seven ways to merge dictionaries Mention the use of ** Unpackable Dictionary .

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]
  1. Use extend
    In the dictionary , Use update Can be updated in place , And in the list , Use extend Self expansion of the list can be realized .
>>> list01 = [1,2,3]
>>> list02 = [4,5,6]
>>>
>>> list01.extend(list02)
>>> list01
[1, 2, 3, 4, 5, 6]
  1. Use list derivation
    Python For the generated list 、 aggregate 、 Dictionaries , There is a very Pythonnic Writing .

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]
  1. Use heapq
    heapq yes Python A standard module of , It provides the implementation of heap sorting algorithm .

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 .

  1. By magic
    In the previous article , A complete introduction to magic .

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]
  1. Use yield from
    In an early article ( Concurrent programming 08| In depth understanding of yield from grammar ), I introduced in detail yield from Meaning and usage .

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


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved