Yield Expression learning in python, pythonyield expression
In python, there is a slightly strange expression called yield expression. This article will explore what this is. Step by step.
Iterable
Copy codeThe Code is as follows:
Mylist = [1, 2, 3]
For item in mylist:
Print str (item)
Mylist is a list. We can retrieve each item one by one. This process is called iteration. Like list, you can use "... In ..." Objects that are traversed in turn are called iterable. Other iterable objects include string, tuple, and dict. One feature of iterable is that all items are stored in the memory, which leads to some inconveniences and disadvantages, and leads to generator (mentioned later ).
List comprehension (list derivation)
Copy codeThe Code is as follows:
Mylist = [x * x for x in range (3)]
The right side of an expression is a short form of a for loop. It is enclosed in [] (called list comprehension). The expression value is a list. We can use for… as a normal list... In ..." Traverse its elements, such:
Copy codeThe Code is as follows:
For item in mylist:
Print str (item)
Generator
Generator
Slightly modify the list comprehension:
Copy codeThe Code is as follows:
Mygenerator = (x * x for x in range (3 ))
For item in mygenerator:
Print item
We can see that the expression is not a list, but a generator.
Generator also belongs to iterable, but its calling method is very special.
Yield
Copy codeThe Code is as follows:
Def creatGenerator ():
Mylist = range (3)
For x in mylist:
Yield x * x
Mygenerator = creatGenerator ()
For x in mygenerator:
Print (x)
Yield uses the same method as return. But (the focus is on ):
Usage of yield in python
Yield is used to save the execution status of the current program.
When you use a for loop, it is calculated every time an element is retrieved.
The yield function is called generator. Like iterator, yield does not calculate all elements at a time, but only one operation at a time, which saves a lot of space. Generator requires the last calculation result for each calculation, so yield is used. Otherwise, the result of the previous calculation will be lost after a return operation.
Therefore, it is totally incorrect to save the list.
For python beginners, I would like to ask questions about yield and so on.
About yield, the kanchi240 downstairs is completely correct. I will not add it.
I want to talk about it. The logic of your yield function may be faulty. The entry parameter nested is an array. When the for sublist in nested in the function is executed, it is unreasonable if the nested is changed and an exception occurs. Maybe the original design is like this.
Def flatten (nested): try: for sublist in nested: for element in flatten (sublist): yield element failed t TypeError: yield nestedif _ name __= = "_ main _": for x in flatten (nested = [1, [2, [3]): print x, the output result of type (x) is
1 <type 'int'>
2 <type 'int'>
3 <type 'int'>
You seem to be a recursive function. The next step is like this.
Flatten (nested = [1, [2, [3])
Sublist = 1
For element in flatten (sublist.
And so on.
I modified the program and it may seem clearer.
Def flatten (nested, level = 0): try: for sublist in nested: print "sublist:", sublist, type (sublist) for element in flatten (sublist, level + 1): print level, "yield", element yield element failed t TypeError: print level, 'yield type error', nested, type (nested) yield nestedif _ name __= = "_ main _": for x in flatten (nested = [1, [2, [3]): print x, the output result of type (x) is
Sublist: 1 <type 'int'> 1 yield type error 1 <type 'int'> 0 yield 11 <type 'int'> sublist: [2, [3] <type 'LIST'> sublist: 2 <type 'int'> 2 yield type error 2 <type 'int'> 1 yield 20 yield 22 <type 'int'> sublist :[...... remaining full text>