Getting started with Python: Generators & iterators

Source: Internet
Author: User
Tags function definition iterable

I. List-generated

now there's a need, list [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], asking you to add 1 to each value in the list, how do you do that? You might think of 2 ways

Two-Force Youth edition

A = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]b = []for i in A:b.append (i+1) print (b)

  

Normal Youth Edition

For index, I in Enumerate (a):    A[index] +=1    print (a)

  

Literary Youth Edition

A = map (lambda x:x+1, a) for I in A:print (i)

  

Forced Youth Edition

A = [i+1 for I in range (a)]print (a)

  This is called: List-generated

Second, generator

With list generation, you can create a list directly, but with memory constraints, the list capacity is limited, and creating a list of 1 million elements not only takes up a lot of storage space, but 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, and in Python, this side loop technique is called the generator: Generator.

There are a number of ways to create a generator.

The first method: simply change the [] form of a list to () and create a generator

L = [x * x for x in range]]print (l) g = (x * x for x in range) print (g)

The differences between L and G are created only at the outermost [] and (), L is a list, and G is a generator

We can print out every element of the list directly, but how do we print out every element of generator?

If you want to print one, you can get the next return value for generator by using the next () function:

>>> Next (g) 0>>> next (g) 1>>> next (g) 4>>> next (g) 9>>> next (g) 16>> > Next (g) 25>>> next (g) 36>>> next (g) 49>>> next (g) 64>>> next (g) 81>>> Next (g) Traceback (most recent):  File "<stdin>", line 1, in <module>stopiteration

  Generator saves the algorithm, each time the call next(g) computes the value of the next element, until the last element is computed, and g no more elements are thrown when the StopIteration error occurs.

Use the For loop output:

g = (x * x for x in range) for N in G:    print (n)

Output:

0149162536496481

  After we create a generator, we basically never call next (), but instead iterate over it with a for loop and don't need to care about Stopiteration's error.

Generator is very powerful. If the algorithm is more complex, it can also be implemented by using a function that is not possible with a For loop that resembles a list-generated type.

For example, the famous Fibonacci sequence (Fibonacci), except for the first and second numbers, can be summed up by the top two numbers:

1, 1, 2, 3, 5, 8, 13, 21, 34, ...

The Fibonacci sequence is not written in a list, but it is easy to print it out with a function:

def fib (max):    N, a, b = 0, 0, 1 while    n < max:        print (b)        A, B = b, a+b        n = n + 1    return ' done ' fi B (10)

Output:

11235813213455

  

Looking closely, it can be seen that the fib function is actually a calculation rule that defines the Fibonacci sequence, which can be derived from the first element, and the subsequent arbitrary elements, which are actually very similar to generator.

In other words, the above functions and generator are only a step away. To turn fib a function into a generator, just print(b) change yield b it to:

def fib (max):    N, a, b = 0, 0, 1 while    n < max:        #print (b)        yield B        A, B = b, a+b        n + = 1    retu RN ' Done '

  This is another way to define generator. If a function definition contains a yield keyword, then the function is no longer a normal function, but a generator:

F=fib () print (f) Output: <generator object fib at 0x0000000002536f68>

The most difficult thing to understand here is that the generator is not the same as the execution flow of the function. The function is executed sequentially, the return statement is encountered, or the last line function statement is returned. The function that becomes generator, executes at each call to next (), encounters a yield statement return, and is again called by next () to continue execution from the last yield statement returned.

data = fib (data) print (data.__next__ ()) print (data.__next__ ()) print (' doing something Else ') print (data.__next__ ()) Print ( data.__next__ ()) print (data.__next__ ()) print (data.__next__ ())

Output:

<generator object fib at 0x0000000002556fc0>11 do something else 2358

  上面fibexample, we continue to call in the loop process yield , will continue to interrupt. Of course, you have to set a condition for the loop to exit the loop, or it will produce an infinite sequence. Similarly, after changing a function to generator, we basically never use it next() to get the next return value, but instead use the for loop directly to iterate:

For N in fib:    print (n)

 However, when you call generator with a For loop, you find that you cannot get the return value of the generator return statement. If you want to get the return value, you must catch the Stopiteration error, and the return value is contained in the value of stopiteration:

g = Fib (Ten) while True:    try:        x = Next (g)        print (' G: ', x)    except stopiteration as E:        print (' Generator Return value: ', e.value) break        

Output:

G:1g:1g:2g:3g:5g:8g:13g:21g:34g:55generator return Value:done

  The effect of concurrency can also be achieved in single-threaded case with yield

Import time__author__ = ' Mike ' def Consumer (name):    print ('%s ready to eat buns! '% name ') while    True:        baozi = yield        Print (' Bun [%s] came, eaten by [%s]! '% (Baozi,name)) def producer (name):    C = Consumer (' A ')    c2 = consumer (' B ') c.__next__    ()    C2.__next_ _ ()    print (' Start ready to make buns! ')    For I in range:        time.sleep (1)        print ("Made 2 buns!")        C.send (i)        c2.send (i) Producer ("Mike")

  

Three, iterators

We already know that for there are several types of data that can be directly acting on a loop:

A class is a collection of data types:,,,, list tuple dict set str ;

The class is: generator , including the generator and yield the generator function with the band.

These objects, which can be directly applied to for the loop, are called iterative objects: Iterable .

You can use to isinstance() determine whether an object is an Iterable object:

From collections Import Iterableprint (Isinstance ([], iterable) print (Isinstance ({}, iterable)) print (Isinstance (' ABC ', iterable)) Print (Isinstance ((x for X in range), iterable))

  

The generator can not only be used for a for loop, but it can also be called by the next () function and return the next value until the last throw Stopiteration error indicates that the next value cannot be returned again.

An object that can be called by the next () function and continually returns the next value is called an iterator: Iterator.

You can use Isinstance () to determine whether an object is a iterator object:

Print (Isinstance ([], Iterator)) print (Isinstance ({}, Iterator)) print (Isinstance (' abc ', Iterator)) print (Isinstance ( (X for X in range), Iterator))

Output:

Falsefalsefalsetrue

  

Generators are Iterator objects, but,, list dict str Though Iterable they are, they are not Iterator .

Turn list , dict and str wait for the Iterable Iterator function to be used iter() :

Print (Isinstance (ITER ([]), Iterator)) print (Isinstance (ITER ({}), Iterator))

Output

Truetrue

  

Why list , dict , str etc. data types are not Iterator ?

This is because the Python Iterator object represents a data stream, and the iterator object can be next() called by the function and will return the next data continuously until there is no data to throw an StopIteration error. You can think of this data stream as an ordered sequence, but we can't know the length of the sequence in advance, only by continuously using the next() function to calculate the next data on demand, so Iterator the calculation is lazy, and it will only be calculated when the next data needs to be returned.

IteratorIt can even represent an infinitely large stream of data, such as the whole natural number. Using list is never possible to store all natural numbers.

Summary

Any object that can be used for for the loop is a Iterable type;

All objects that can be used for next() functions are Iterator types, which represent a sequence of lazy computations;

Collection data types such as list , dict ,, and str so on are Iterable not Iterator , however, you can iter() get an object from a function Iterator .

Getting started with Python: Generators & iterators

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.