python-Basics-List generation, generators, and iterators

Source: Internet
Author: User
Tags iterable

I. List-generated

The list generation, which is the comprehensions, is a very simple and powerful build of Python built-in that can be used to create lists.

For example, to generate a list, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] you can use list(range(1, 11)) :

List (range (1,11)) [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

But what if you want to build [1x1, 2x2, 3x3, ..., 10x10] ? Method One is the loop:

For x in range (1, one):    l.append (x * x)    l[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

But the loop is too cumbersome, and the list generation can use a line of statements instead of loops to generate the list above:

[x * x for x in range (1, 11)] [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

You can also use a two-layer loop to generate a full array:

[M + N for m in ' ABC ' for n in ' XYZ '] [' AX ', ' AY ', ' AZ ', ' BX ', ' by ', ' BZ ', ' CX ', ' CY ', ' CZ ']

Three-and three-storey loops are rarely used.

Using a list-generated formula, you can write very concise code. For example, listing all the file and directory names in the current directory can be implemented in one line of code:

Import os[d for D in Os.listdir ('. ')] ['. Git ', '. Idea ', ' exam ', ' learning ']
Second, generator

With list generation, we can create a list directly. However, with memory limitations, the list capacity is certainly limited. 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.

There are a number of ways to create a generator. The first method is simple, as long as a list of the generated formula is [] changed () to create a generator:

L = [x * x for x in range (1,11)]g = (x * x for x in range (1,11))

The L difference between creating and making is g only the outermost [] and, a () L list, and g 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 out one, you can next() get the next return value for generator by using a 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

We have said that generator saves the algorithm, each time it is called next(g) , computes g the value of the next element until the last element is computed, and when there are no more elements, the error is thrown StopIteration .

Of course, this constant invocation is next(g) so perverted that the correct approach is to use for loops because the generator is also an iterative object :

>>> g = (x * x for x in range) >>> for n in G: ...     Print (n) ... 0149162536496481

So, after we create a generator, we basically never call next() it, but iterate over it through the for loop and don't need to be concerned about StopIteration the error.

Generator is very powerful. If the calculated algorithm is more complex, and the loop with similar list generation for cannot be implemented, it can also be implemented with functions.

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

The above function can output the number of the first n of the Fibonacci sequence:

>>> fib (11235813213455done)

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,a+b        n + = 1    Return ' Done '

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 statement is encountered return or the last line of the function is returned. The function that becomes generator, executes at each invocation next() , encounters a yield statement return, and executes again from the last statement returned yield .

You can also achieve the effect of concurrency in single-threaded situations by using yield:

#_ *_coding:utf-8_*___author__ = ' Alex Li ' import timedef Consumer (name):    print ("%s ready to eat Buns!"%name) while    true:< C2/>baozi = yield       print ("Bun [%s] came, was [%s] eaten!"% (Baozi,name)) def producer (name):    C = Consumer (' A ')    C2 = Consumer (' B ')    c.__next__ ()    c2.__next__ ()    print ("Lao Tzu began to prepare buns!")    For I in range:        time.sleep (1)        print ("Made 2 buns!")        C.send (i)        c2.send (i) producer ("Alex")
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, such as,,, list tuple , and dict set str so on;

One is generator to include the generator and yield the generator function with the band.

These objects, which can be directly acting on for a loop, are collectively known as an iterative object : Iterable .

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

>>> from Collections Import iterable>>> isinstance ([], iterable) true>>> isinstance ({}, iterable) true>>> isinstance (' abc ', iterable) true>>> isinstance ((x for X in range), iterable) True >>> isinstance (iterable) False

The generator can not only be used for for loops, but it can also be next() called by the function and return the next value until the last throw StopIteration error indicates that the next value cannot continue to be returned.

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

Generators are Iterator objects, but,, list dict str Though 可迭代对象 they are, they are not 迭代器 .

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

>>> from Collections Import iterator>>> isinstance ((x to X in range), Iterator) true>>> are Instance ([], Iterator) false>>> isinstance ({}, Iterator) false>>> isinstance (' abc ', Iterator) False

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

>>> Isinstance (ITER ([]), Iterator) true>>> isinstance (ITER (' abc '), Iterator) True

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() :

>>> Isinstance (ITER ([]), Iterator) true>>> isinstance (ITER (' abc '), Iterator) True

You may ask, why, list dict , str etc. data types are not 迭代器 ?

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 .

The Python for loop is essentially implemented by calling next() functions, such as:

For x in [1, 2, 3, 4, 5]:    Pass

is actually exactly equivalent to:

# Get Iterator Object first: it = iter ([1, 2, 3, 4, 5]) # loop: While True:    try:        # Get Next value:        x = Next (it)    except Stopiterati On:        # exit Loop break when encountering Stopiteration        

python-Basics-List generation, generators, and 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.