Python: iterators and generators

Source: Internet
Author: User
Tags iterable

One, iterative objects and iterators
1. The concept of iteration
The result of the last output is the initial value of the next input, and the repeating process is called an iteration, one iteration at a time, and the result of each iteration is the initial value of the next iteration

Note: Loops are not iterations

While True: #只满足重复, and therefore not an iteration
Print (' ====> ')

2. Objects that can be iterated
The built-in __iter__ method is an iterative object.
List is an iterative object, Dict is an iterative object, and set is also an iterative object.

[1,2].__iter__ ()
' Hello '. __iter__ ()
(__iter__).

{' A ': 1, ' B ': 2}.__iter__ ()
{1,2,3}.__iter__ ()

For example:


x = [1, 2, 3]
y = iter (x)
z = ITER (x)
Print (Next (y))
Print (Next (y))
Print (Next (z))
Print (type (x))
Print (Type (y))

Output

1
2
1
<class ' list ' >
<class ' List_iterator ' >

As shown

Here x is an iterative object, and Y and Z are two independent iterators that hold a state within the iterator that is used to record where the current iteration is located, so that the correct element can be obtained at the next iteration.

Iterators have a specific type of iterator, such as List_iterator,set_iterator. An iterative object implements the __iter__ method, which returns an Iterator object.

3. iterators
1. Why do I have iterators?
For data types that do not have an index, you must provide an iterative approach that does not depend on the index.

2. Iterator Definition:
Iterator: An iterative object executes the __iter__ method, resulting in an iterator, an iterator object having a __next__ method

It is a stateful object, he can return the next value in the container when you call the next () method, any object that implements the __iter__ and __next__ () methods is an iterator, __ITER__ returns the iterator itself, __NEXT__ returns the next value in the container. Throws a Stopiteration exception if there are no more elements in the container

3. Implementation of Iterators
Cases:


I=[1,2,3].__ITER__ ()

Print (i) #迭代器

Print (i.__next__ ())
Print (i.__next__ ())
Print (i.__next__ ())
#print (i.__next__ ()) #抛出异常: stopiteration

Output

<list_iterator Object at 0x1019c3eb8>
1
2
3
Do two things each time you call the next () method:

Modify the state for the next call to the next () method
Generates a return result for the current call
An iterator is like a lazy-loaded factory that returns a value when someone needs it, and waits for the next call when it is not called.

4. How to judge an iterator object and an iterator object

From collections Import Iterable,iterator
' abc '. __ITER__ ()
(). __iter__ ()
[].__iter__ ()
{' A ': 1}.__iter__ ()
{1,2}.__iter__ ()

F=open (' A.txt ', ' W ')
F.__ITER__ ()

#判断是否为可迭代对象, the following are
Print (Isinstance (' abc ', iterable))
Print (Isinstance ([],iterable))
Print (Isinstance ((), iterable))
Print (Isinstance ({' A ': 1},iterable))
Print (Isinstance ({1,2},iterable))
Print (Isinstance (f,iterable))

#判断是否为迭代器, only files are
Print (Isinstance (' abc ', Iterator))
Print (Isinstance ([],iterator))
Print (Isinstance ((), Iterator))
Print (Isinstance ({' A ': 1},iterator))
Print (Isinstance ({1,2},iterator))
Print (Isinstance (f,iterator))

Output


True
True
True
True
True
True
False
False
False
False
False
True

Iterate objects: Only the __iter__ method, the iterator object that the method gets executed

Iterators: With __iter__ and __next__ () methods

Note: For an iterator object, the __iter__ method is executed, and the result is still itself

5. Advantages and disadvantages of iterators
Advantages:
1. Provides an iterative approach that does not rely on subscript
2. More memory savings on the drop iterator itself

Disadvantages:
1. Unable to get the length of the iterator object
2. The value of the sequence type is flexible, is a one-time, can only be taken back to the value, not forward

Second, generator
1. Definition
The generator (generator) is a special iterator whose implementation is simpler and more elegant, and yield is the key to the generator's implementation of the __next__ () method. It acts as a paused recovery point for the generator, can be assigned to a yield expression, or it can return the value of a yield expression.

In other words, yield is a syntactic sugar, and the internal implementation supports the iterator protocol, while the yield is a state machine that maintains the status of suspend and continue.

Yield Features:
1. The equivalent of encapsulating __iter__ and __next__ for functions
2.return can only return one value at a time, the function terminates, and yield returns multiple values, each time it is returned, the function is paused, and next will continue from the last paused position.

Cases:


def counter (n):
Print (' Start ... ')
I=0
While I < n:
Yield I
I+=1
Print (' End ... ')


G=counter (5)
Print (g)
Print (Next (g))
Print (Next (g))
Print (Next (g))
Print (Next (g))
Print (Next (g))
# Print (Next (g)) #会报错

Output

Start ...
0
1
2
3
4

2. Generator functions
Generator functions: Regular function definitions, however, use the yield statement instead of the return statement to return the result. The yield statement returns one result at a time, in the middle of each result, suspends the state of the function so that the next time it leaves, the execution continues;

Normal function return returns


def lay_eggs (num):
Egg_list=[]
For-egg in range (num):
Egg_list.append (' egg%s '%egg)
Return egg_list

Yikuangdan=lay_eggs (#我们拿到的是蛋)
Print (Yikuangdan)

Output

[' Egg 0 ', ' egg 1 ', ' Egg 2 ', ' Egg 3 ', ' Egg 4 ', ' Egg 5 ', ' Egg 6 ', ' Egg 7 ', ' Egg 8 ', ' Egg 9 ']

Iterator functions


def lay_eggs (num):
For-egg in range (num):
res= ' Egg%s '%egg
Yield Res #生成器关键语法
Print (' End an egg ')

Laomuji=lay_eggs (#我们拿到的是一只母鸡)
Print (Laomuji)
Print (laomuji.__next__ ()) #迭代 Egg 0
Print (laomuji.__next__ ()) #蛋1
Print (laomuji.__next__ ()) #蛋2
Egg_l=list (Laomuji)
Print (egg_l)

Output


Egg 0
Finish an egg.
Egg 1
Finish an egg.
Egg 2
Finish an egg.
Finish an egg.
Finish an egg.
Finish an egg.
Finish an egg.
Finish an egg.
Finish an egg.
Finish an egg.
[' Egg 3 ', ' Egg 4 ', ' Egg 5 ', ' Egg 6 ', ' Egg 7 ', ' Egg 8 ', ' Egg 9 ']

3. Generator expressions
Builder expression: Similar to list derivation, however, the generator returns an object that produces results on demand, rather than building a list of results at a time;
Food=yield food_list
#g. Send (' Food1 '), first pass the food1 to yield, the yield is assigned to food, and then return to Food_list, and then proceed down until the yield is met again, then return the yield after the return value to Food_list


Cases

Note: The start generator cannot send a non-null value


Def eater (name): #协程函数
Print ('%s ready to eat '%name)
Food_list=[]
While True:
Food=yield food_list #装饰器表达式
Food_list.append (food)
Print ('%s start to eat%s '% (Name,food))


G=eater (' hexin ')
Print (g) #生成器

Print (G.send (' food1 ')) #传值

Output

Traceback (most recent):
<generator object Eater at 0x1049030f8> #生成器对象
File "/users/hexin/pycharmprojects/py3/day5/2.py", line +, in <module>
Print (G.send (' food1 '))
Typeerror:can ' t send non-none value to a just-started generator #开始生成器不能send非空值

After initialization

Def eater (name): #协程函数
Print ('%s ready to eat '%name)
Food_list=[]
While True:
Food=yield food_list #装饰器表达式
Food_list.append (food)
Print ('%s start to eat%s '% (Name,food))


G=eater (' hexin ')
Print (g) #生成器
Next (g) #等同于 G.send (None), initialize

Print (G.send (' food1 '))

Output

<generator Object Eater at 0x107cde258>
Hexin ready to eat
Hexin start to eat food1
[' Food1 ']

To prevent forgetting to initialize, the adorner can be initialized with the following

def deco (func): #初始化函数
def wrapper (*args,**kwargs):
Res=func (*args,**kwargs)
Next (Res) #等同于 g.send (None), initialize
return res
Return wrapper

@deco #用初始化函数装饰器, call the initialization function
Def eater (name): #协程函数
Print ('%s ready to eat '%name)
Food_list=[]
While True:
Food=yield food_list #装饰器表达式
Food_list.append (food)
Print ('%s start to eat%s '% (Name,food))


G=eater (' hexin ')
# Print (g) #生成器
# Next (g) #等同于 G.send (None), initialize

Print (G.send (' food1 '))
Print (G.send (' food2 '))
Print (G.send (' food3 '))

Output


Hexin ready to eat
Hexin start to eat food1
[' Food1 ']
Hexin start to eat food2
[' Food1 ', ' food2 ']
Hexin start to eat food3
[' Food1 ', ' food2 ', ' food3 ']

Python: iterators and generators

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.