Python list generation and generator

Source: Internet
Author: User
Tags iterable python list

1 List Builder: List generation is an expression that is used to generate a specific syntactic form of the list.

1. Structure of the underlying statement: [exp for Iter_var in iterable example: A=[f (x) for x in range (Ten)]

2. Working process:

Iterates over each element in the iterable, assigns the result to iter_var each iteration, and then obtains a new calculated value through exp; Finally, all the calculated values obtained by EXP are returned as a new list.

The equivalent of this process:L = [] for iter_var in iterable:

L.append(exp)

Detailed Description: https://www.cnblogs.com/yyds/p/6281453.html

"3" Generator:

1. Function: continuously generate new data according to an algorithm until the end of a specified condition is satisfied.

There are two ways to construct a generator:

    • Generated using a similar list-generated form (2*n + 1 for N in range (3, 11))
    • Use a function that contains yield to generate
The execution process of the generator:

During execution, the yield keyword is interrupted execution, and the next call continues from where it was last interrupted.

Characteristics of the generator:
    • The corresponding data is generated only when it is called
    • Record the current location only
    • Only next, not prev.

Call of the generator

There are two ways to call the generator to produce a new element:

    • Call the built-in next () method
    • Using loops to traverse a generator object (recommended)
    • Call the Send () method of the Builder object
#Print (List range )#s= (x*2 for x in range )#print (s) #<generator object <genexpr> at 0x000001ebd2e2cfc0> Builder Objects#For I in S:###print (s.__next__ ())#print (Next (s), i) #等价于s. __next__ () Remove an element#print (Next (s))#print (Next (s))#The generator is an iterative object (iterable)#two ways to create a generator#1 (x*2 for x in range (5))#2.yieIddeffoo ():Print('OK')    yield1Print('Ok2')    yield2g=foo ()#Print (g)#For i in Foo ():#print (i)#Next (g)#Next (g)#What is an iterative object (whether there is a _iter_)l=[1,2,3]l.__iter__()defFID (max): N,before,after=0,0,1 whilen<Max:yieldBefore,after=after,before+After N=n+1FID (5)

Python list generation and generator

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.