Python co-function application list generation builder expression

Source: Internet
Author: User
Tags generator generator lenovo

First, the Knowledge point collation:

1, can iterate: The object has the _iter_ method is an iterative object

Iterator: Object. _iter_ () The result is an iterator
Characteristics of iterators:
Iterator. _next_ () Remove a value

Advantages:
1. Provides a uniform way of iterating over objects, independent of the index
2. Lazy Calculation
Disadvantages:
1. Unable to get the length of the iterator
2. One-time, can only be back to value, can not go forward, not like the index to get the value of a position

2, Generator: function with the yield keyword, then the result of this function is the generator

The essence of the generator is the iterator

def func ():
N=0
While True:
Yield n
N+=1
g = func ()
Next (g)

3, summarize the function of yield:
1, equivalent to the _iter_ and _next_ methods encapsulated inside the function
2, with return, return can only be returned once, and yield can return multiple times
3, function pause and continue running state is saved through yield

4. The expression form of yield: food = yield

Def eater (name):
Print ("%s start to eat"%name)
While True:
Food = yield
Print ("%s eat%s"% (Name,food))
E = Eater ("ZhejiangF4")
Next (E)
E.send ("AAA")

5. The difference between E.send and next (e)

#1. If the yield in the function is an expression, you must first next (e)

#2. In common, both allow the function to continue at the last paused position, except that send will pass a value to yield when the next execution of the code is triggered.

Second, the application of the Generator association function

1, write an adorner, without changing the original code on the basis of the function, directly to the main function to pass value! (The first step of the co-function requires the next () trigger, which will trigger the write to the adorner!) )

1 def F (func):  #定义装饰器 2     def F1 (*args,**kwargs): 3         res = func (*args,**kwargs) 4         Next (res)  #触发主函数 5< C5/>return Res 6     return F1 7 @f 8 def Eater (name):   #主函数 9     print ("%s start to eat"%name) ten while     True:11
   food = yield12         print ("%s eat%s"% (Name,food)) e  = Eater ("ZhejiangF4") e.send ("Something")   #直接传值

Execution Result:

1 zhejiangF4 start to eat2 zhejiangF4 eat something

# #在IDE上加上断点, Debug run View # #

2, recursive directory, filter files with "Python" content files, and then print these files. This part of the code to implement the function, involving the idea of process-oriented programming! Each of the defined functions is interlocking, like a complete line!

Process-oriented Programming idea: Pipelining programming idea, in the design process, need to design the whole process

#优点:
1. Clearer architecture
2, simplify the complexity of the program
#缺点:
1, scalability is extremely poor, so that the process-oriented application scenario is: Do not need to change the software frequently.

 1 Import os,time 2 def init (func): 3 def wrapper (*args,**kwargs): 4 res = func (*args,**kwargs) 5 Next         (RES) 6 return res 7 return wrapper 8 9 @init10 def search (target): 11 ' absolute path to file found ' while true:13         Dir_name=yield14 #print (' Shop search start Product: Absolute path to file ') Time.sleep (1) g = Os.walk (Dir_name) 17 For I in G:18 to J in i[-1]:19 File_path = "%s\\%s"% (i[0],j) target.         Send (File_path) @init22 def opener (target): 23 ' Open file, get file handle ' while true:25 File_path = yield26 #print (' Workshop opener start production: File Handle ') Time.sleep (1) with open (File_path) as f:29 target.send ((file _path,f)) @init31 def Cat (target): 32 ' Read file contents ' while true:34 file_path,f = yield35 #print (' Workshop CA T start production: One line of the file ') Time.sleep (1) PNs for lines in f:38 target.send (file_path,line)) @init40 D EF grep (pattern,target):41 ' Filter a line with Python ' while true:43 file_path,line = yield44 #print (' shop grep starts producing products: a text that contains the Python line of content Path ') Time.sleep (0.2), if pattern in line:47 target.send (file_path) @init49 def printer () : 50 ' Print file path ' while true:52 File_path = yield53 #print (' Workshop printer start production product: Get final product ') Sleep (1) print (file_path) + G = Search (Opener (grep (' Python ', Printer ()))) G.send (' G:\\zhang ')

Execution Result:

1 G:\zhang\a3.txt2 G:\zhang\a1\a1.txt3 G:\zhang\a2\a2.txt

Iii. List-Generated

1. Origin

In the case of actual programming, we often need to generate some lists. In addition to the relatively inefficient use of a for loop to append a list, another good way is to:

Python provides us with a very powerful way to create a list.

2. Grammar
[Expression for item1 in Iterable1 if Condition1
For item2 in Iterable2 if Condition2
For item3 in Iterable3 if Condition3
For itemn in Iterablen if Conditionn]
In layman's terms, the list generation is composed of three parts: Of course, before each write should be given [], and then added inside.
1.expression refers to the element (parameter, variable) to be generated, placed at the front
2. Follow the For loop
After the 3.for loop, you can also add an if condition to be judged for filtering.
The actual use of the process, if a for loop does not complete the problem, you can also nest down.
1) Simple code example:
1  egg_list=[]2 for I in range: 3     egg_list.append ("egg%s"%i) 4 print (egg_list) 5 6 l=["egg%s"%i for I in range (10 )]7 print (L)
Execution Result:
1 [' egg0 ', ' egg1 ', ' egg2 ', ' egg3 ', ' egg4 ', ' egg5 ', ' egg6 ', ' Egg7 ', ' Egg8 ', ' egg9 ']2 [' egg0 ', ' egg1 ', ' egg2 ', ' egg3 ', ' Egg4 ' ', ' egg5 ', ' egg6 ', ' Egg7 ', ' Egg8 ', ' egg9 ']
2) It's a little bit complicated, but it's understandable.
1 #将l and S each element is taken out, forming a new tuple, saving all the results in the list 2 L = [1,2,3,4] 3 s = "Hello" 4 l1 = [(num,s1) for NUM in L if Num >3 for S1 i N S] 5 print (L1) 6  7 L2 = [] 8 for NUM1 in L:9     if num1 >3:10         for S2 in s:11             t = (num1, s2)             L2. Append (t) print (L2)
Execution Result:
1 [(4, ' H '), (4, ' E '), (4, ' L '), (4, ' L '), (4, ' o ')]2 [(4, ' H '), (4, ' L '), (4, ' L '), (4, ' O ')]

By comparison, although the above two ways can be implemented, but it can be very obvious that: the use of traditional loops, to write code is very cumbersome and complex.
With the use of list generation, the same content can be quickly generated by a list to implement the function of the code, and the code is very concise writing.

3) Another example: the absolute path of the read file

① Code:
1 Import os 2 G = Os.walk ("G:\\zhang")  #拿取文件路径下所有的文件 3 #print (g)  #g是一个生成器 4 L = [] 5 for I in G: #获取所有文件的绝对路径 6
    #print (i)  #路径整体以元组的形式打印出来, within the tuple is a list (file path, file name, files) 7 for     J in I[-1]:  #拿取有文件的路径 8         file_path = "%s\\%s"  % (I[0], j) 9         l.append (file_path) print (L) one-page g = Os.walk ("G:\\zhang") L1 = ["%s\\%s"% (I[0], J) for I in G for J in i[-1]]14 print (L1)

② Execution Results:
1 [' G:\\zhang\\a3.txt ', ' g:\\zhang\\a1\\a1.txt ', ' g:\\zhang\\a2\\a2.txt ']

Iv. Generator Expressions

1. Definition:
Generator expression, I personally think it's better to call the list Builder, to change the list expression and become a generator.
And this change is very simple, that is, the outside [] replaced () to create a generator.
With list generation, we can create a list directly. But by the memory limit, the list capacity is bound to be limited, at the same time so huge data flow, all of a sudden to take out what the machine can not stand.
Also, creating a list of 1 million elements takes up a lot of storage space, and if we just need to access the first few elements, the vast majority of the space behind it is wasted.
So, if the list element can be calculated according to an algorithm, can we continue to calculate the subsequent elements in the process of the loop? This eliminates the need to create a complete list, which saves a lot of space.
In Python, this side loop computes the mechanism, called the generator: Generator.
Judging by the understanding of the generator that was learned yesterday: the generator generator holds the algorithm, triggering the value each time through next (), and takes only one element at a time until the last element is calculated.
When there are no more elements, the Stopiteration error is thrown. We can iterate over it with a for loop, and we don't need to care about Stopiteration's error.

This generator is often used to process files and read large amounts of data in a database .
1, Simple code example:
1 #还是下蛋的例子 (... It's not like a chicken. ) 2 l=[' egg%s '%i for I in range (+)] 3 print (L) 4  5 g=l= (' egg%s '%i for I in range (100000000000000000000000000000000 0000)) 6 print (g) 7 print (Next (g)) 8 print (Next (g)) 9 for I in G:10     print (i)
Execution Result:
2. Examples of code handling files:
1 #处理文件, remove the space in each line of the file 2 #传统处理方式, if the data is very large, the memory burst instantly ... 3 F=open (' A.txt ') 4 l=[] 5 6 for line in  f:7     Line=line.strip () 8     l.append (line) 9 print (L) ten F=open (' A.txt ') F.seek (0) L1=[line.strip () for line in f]14 print (L1), F=open (' A.txt ') f.seek (0) g= (Line.strip () for line I  n f) print (g) print (Next (g)) #list (iterative object) to  convert the iterator to a list of F=open (' A.txt ') g= (Line.strip () for line in F) 26 L=list (g) Print (L)
Execution Result:
View Code
3. Application: Declarative Programming
1) Sum function sum () calculates the value of the data that can be iterated
1 #1, Sum function sum () computes the value of the data that can be iterated by 2 print (SUM ([1,2,3,4])) #直接对列表求和3 nums_g= (I for I in range (3)) #生成器4 print (sum (nums_g)) #求和
Execution Result:
1 102 3
2) Calculate the total price of the shopping list
1 # Calculation of shopping list Total Price 2 # 1, traditional way 3 money_l=[] 4 with open (' B.txt ') as F:5 for line in     f:6         goods=line.split ()  #将文件中的每行 Separated by a space and saved as a list 7         res=float (Goods[-1]) *float (Goods[-2]) Number of #求和 * unit Price here Note the conversion of the data type STR-FLOAT 8         Money_ L.append (RES)  #生成一个总价的列表 9 print (money_l)  #打印列表10 print (sum (money_l)) #求总价11 #12 # 2, List Builder method  Replace the above code with declarative programming for the F=open (' B.txt ') g= (float (line.split () [-1]) *float (Line.split () [-2]) for line in F) (SUM (g)) 16 #
Execution Result:
1 [30.0, 1000000.0, 6000.0, 90000.0, 30.0]2 1096060.03 1096060.0
3) The function of database query (file data, string) is the form of [{},{}] form, List set dictionary.
1 res=[] 2 with open (' B.txt ') as F:3 for line in     f:4         # Print (line) 5         l=line.split () #把每行处理成列表 6         # Print ( L) 7         d={}  #先定义一个空字典 8         d[' name ']=l[0]  #往字典内赋值 9         d[' price ']=l[1]  #往字典内赋值10         d[' count ']= L[2]  #往字典内赋值11         res.append (d)  #将新创建的字典写到列表中12 print (res)  #打印结果13 #14 # Builder expression handles the B.txt ') as F:16     res= (Line.split () for line in F)  #得到一个列表生成器 large list of all content in the file in the     #print (res) #查看类型 Generator 18     dic_g= ({' name ': i[0], ' price ': i[1], ' count ': i[2]} for I in res)  #对迭代器进行取值, get each small list, form a new dictionary, and store it in the new list 19     print (dic_g) #查看类型  generator     apple_dic=next (dic_g)  #取第一值 If you  know what the first one is,     print (apple_ dic[' Count '])
Execution Result:
1 [{' name ': ' Apple ', ' price ': ' + ', ' count ': ' 3 '}, {' name ': ' Tesla ', ' price ': ' 1000000 ', ' count ': ' 1 '}, {' name ': ' Mac ', ' P Rice ': ' + ', ' count ': ' 2 '}, {' name ': ' Lenovo ', ' price ': ' 30000 ', ' count ': ' 3 '}, {' name ': ' Chicken ', ' price ': ' Ten ', ' Coun T ': ' 3 '}]2 <generator object <genexpr> at 0x00000000028eb360>3 3

Here's a very interesting question, as we learned yesterday, we know that the file itself is an iterator.

Next () will close the file after the value is taken. You will not be able to get the value again, so I am unable to read the file close error.
Therefore, if you call the file, it is recommended to use F = open ("B.txt") or next () to trigger the value, and put it in the inside.

4) Take out the unit price >10000 roughly unchanged, just a list of each line, format conversion into a dictionary when the filter, take out the content of the conditions to meet
1 # Take out the unit price >10000  roughly unchanged, just a list of each line, format converted into a dictionary when filtering, remove content satisfying the condition 2 with open (' B.txt ') as F:3     res= (Line.split () for Line in F) 4     # Print (res) 5     dic_g= ({' name ': i[0], ' price ': i[1], ' count ': i[2]} for I in res if float (i[1]) > 10000) 6     Print (dic_g) 7     #print (List (dic_g))  #直接取值8     for  i in Dic_g:  #for循环取值9         Print (i)

Execution Result:

1 <generator Object <genexpr> at 0x00000000026bb3b8>2 {' name ': ' Tesla ', ' price ': ' 1000000 ', ' count ': ' 1 '}3 {' n Ame ': ' Lenovo ', ' price ': ' 30000 ', ' count ': ' 3 '}

Python co-function application list generation builder expression

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.