Python container, iterator, generator

Source: Internet
Author: User

1. Containers, iterative objects, iterators, generator concepts

1. Container: A data structure that stores many elements. Usually stored in memory (iterators, generators are special cases) can use in to determine whether an element exists in the object is a container

For example: A container is like a box, which can store a lot of things, I can go to this box to access things, you can tell if there is something in this box

2. An iterative object: objects that can be used to become iterators with ITER () are iterative objects, most of which are iterative objects (Str,set,list,tuple, files,sockets of open states, and so on)

3. Iterator: It is a stateful object that holds the current record state and can return the next value of the container when using the next () function call. If there are no more elements in the container, the stopiteration exception is thrown.

Any object that implements the __iter__ () and next() method is an iterator, __ITER__ returns the iterator itself, and the __next__ object returns the next value. The generator is another form of iterator

An iterator is an object that implements the factory pattern, and it returns you every time you ask for the next value. For example, the Itertools function returns an iterator object.

4. Generator: Using the yield keyword, Python treats this function as a generator. It returns an iterative object that you can use next () to access its values.

5. How do I use iterators?

1. The method is to use the for: In. Syntax sugar structure to traverse iterators.
The For default call __iter__ returns an iterative object, and then calls the next () method to invoke the __next__ method of the iteration object to get the next value.
If there is no value at the end, the iterator throws an exception, and for will catch the exception and exit the loop.

2. First get the iterator object, such as using the IT = ITER (list) method, to get the next value in call next (IT). No value at the end throws an exception (Stopiteration)

The following diagram shows the relationship between the two of them.

2. Specific examples to demonstrate the above content

Take the Fibonacci sequence for example.

1. Common function implementations

# 普通函数斐波那契数列def fib1(n):    pre, cur = 0, 1    i = 0    while i < n:        print(cur, end=‘ ‘)        pre, cur = cur, cur + pre        i += 1调用: fib1(10)输出:1 1 2 3 5 8 13 21 34 55

2. General function Implementation Improvements

# 普通函数斐波那契数列改进def fib1(n):    result = []    pre, cur = 0, 1    i = 0    while i < n:        result.append(cur)        pre, cur = cur, cur + pre        i += 1    return result    调用:lst = fib1(10)      print(lst)输出:[1, 1, 2, 3, 5, 8, 13, 21, 34, 55]

3. Customize an iterative class to implement the Fibonacci sequence

  class Fib:def __init__ (Self, stop): Self.stop = Stop Self.pre = 0 Self.cur = 1 Self.start = 0 print (' init finish ') def __iter__ (self): print (' return ITER ') return to self def __            Next__ (self): print (' Return next value ') if Self.start < Self.stop:self.start + = 1 x = Self.cur Self.pre, self.cur = self.cur, Self.cur + self.pre return x Else:ra Ise stopiteration () Initializes the class, gets a class instance object F, and can see "init finish" printed out in [1]: F = Fib (Ten) Out[1]:init finish we know that an iterative object can be made via ITER () function into an iterator, we can see from the following call that when we use the ITER () function, it defaults to calling the function's __iter__ () function, returning the Iteration object in [2]: it = iter (f) Out[2]:return ITER when we get the iteration object , we call the next () function to get the value we want, so what does next () do? 1. It calls __next__ () Method 2 in the class. We __next__ () method, return the current value, and save the current value record state and calculate the value to return in the next call in [3] : Next (IT) Out[30]:return next valueout[30]: 1In [3]: Next (IT) Out[30]:return next valueout[30]: 2 If you have been executing next (), it will always return a value, Until the stopiteration () exception is thrown  

4. Using the generator

使用生成器,我们有两种形式:1.创建生成器函数,使用yield关键字2.使用生成器表达式

4.1 Creating a generator function

# 使用yield实现斐波那契数列def fib(n):    pre, cur = 0, 1    i = 0    while i < n:        print(‘call this‘)        yield cur        pre, cur = cur, pre + cur        i += 1        print(‘here‘)从下面的执行结果我们可以看出,使用关键字yield后,调用fib(10)它的返回值是一个生成器In [33]: f = fib(10)In [34]: type(f)Out[34]: generator从上面图我们知道generator is iterator,我们可以使用next()函数获取值,可以看出我们没执行一次next(),好像我们的程序就在yield返回cur,然后停到这不执行,直到下次调用next()函数又继续往下走,不停重复这个过程In [1]: next(f)call thisOut[1]: 1In [2]: next(f)herecall thisOut[2]: 2In [3]: next(f)herecall thisOut[3]: 3

4.2 Generator expression implements Fibonacci sequence

# 从下面我们可以看出,使用()后a是一个generator类型In [1]: a = ( x for x in fib3(10))In [2]: type(a)Out[2]: generatorIn [3]: next(a)call thisOut[3]: 1In [4]: next(a)herecall thisOut[4]: 1In [5]: next(a)herecall thisOut[5]: 2

5. For iterative object traversal, we must think of the For loop

The following specifically explains the next for loop:

1.for循环的一般格式如下:for iter_var in iterable:    suite_to_repeat从上面可以看出,只要是可迭代对象都可以使用它的里面到底做了些什么呢?

For example, let's take a look at the for loop.

Class Fib:def __init__ (Self, stop): Self.stop = Stop Self.pre = 0 self.cur = 1 self.start = 0 print (' init finish ') def __iter__ (self): print (' return ITER ') return to self def __next__ (self ): Print (' Return next value ') if Self.start < Self.stop:self.start + = 1 x = SELF.C ur self.pre, self.cur = self.cur, Self.cur + self.pre return x else:raise stopite Ration () iterates over an iterative object Def fortest (): For X in Fib (4): Print (x) fortest () Import Disdis.dis (fortest) print the following Info: init finishreturn iterreturn next Value1return next Value1return next Value2return next value3 decompile results 108 0 SETU P_loop (To) 2 Load_global 0 (FIB) 4 Load_const 1 ( 4) 6 Call_function 1 8 get_iter >> For_iter (to 2 4) Store_FAST 0 (x) 109 Load_global 1 (print) Load_fast 0 (x)   Call_function 1 pop_top jump_absolute >> Pop_block >> load_const 0 (None) Return_value you can see that there are two very important instructions in the for interior get_it Er,for_iter,get_iter is equivalent to calling ITER (Fib (4), returning an object iterator Itfor_iter equivalent to calling next (it), calling the __next () __ () method inside the class to get the value, looping the process, Until a thrown stopiteration () exception is encountered

Python container, iterator, generator

Related Article

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.