The difference between a python-iterator and a generator

Source: Internet
Author: User
Tags generator

Here are a few points of knowledge: iterators, generators, Yieid

Let's look at the difference between iterators and generators with an example.

#L是个list, you can iterate for a loop, l take it out and store it in memory, and then loop it out many times.
>>> l=[x*x for x in range (3)]>>> for I in L:print (i) 014>>> for n in L:print (n) 014

#把 [] becomes a generator, except that it can only be recycled once, because it is not in memory. It is generating data in real time. >>> Mygenerator = (x*x for x in range (3)) >>> for I in Mygenerator:print (i) 014

In fact, I feel the most bad points have the following:

1, the list function call directly results. And the generator is an object

>>> L = [1,2,3,4,5,6]>>> def xh (): For I in L:print (i) >>> g=xh () 123456

>>> l1=[6,7,8,9,10]>>> def generator (): Yield (L1) for I in L1:print (i) >>> g=generator () > >> Next (g) [6, 7, 8, 9, 10]

Here's a yield comparison, let's feel it.

>>> l1=[6,7,8,9,10]>>> def generator (): Yield (L1) #打印L1的意思 for i in L1:print (i) >>> g= Generator () #这里是一个生成器对象 >>> Next (g) #用next打印出来 [6, 7, 8, 9, ten] #生成器第2个例子 >>> def gen2 (): For I in L1:yield (i ) #打印i的值print (i) >>> g=gen2 () >>> print (g) <generator object Gen2 at 0x0000000002eafd00>>> > For x in G:print (x) 667788991010

2, actually think is also quite simple, yield is just a logo, print the results at once

Classic case: Yang Hui Triangle

#将杨辉三角的每一行看成一个list, write a generator (generator) and continuously output the next line listdef Triangel (n):    l=[1]                                                                 #定义一个list [1] while    True:        Yield L                                                           #打印出该list        l=[l[x]+l[x+1] for x in range (len (L)-1)]        #计算下一行中间的值 (except 1 on both sides)        L.insert (0,1)                                                 # Insert 1        l.append (1) at the beginning                                                 #在结尾添加1        if Len (L) >10:                                                 #仅输出10行            break# generates a generator object, Then output each line with a for loop iteration A=angel (Ten) for I in A:    print (i)
#结果 [1][1, 1][1, 2, 1][1, 3, 3, 1][1, 4, 6, 4, 1][1, 5, 10, 10, 5, 1][1, 6, 15, 20, 15, 6, 1][1, 7, 21, 35, 35, 21, 7, 1][1 , 8, 28, 56, 70, 56, 28, 8, 1][1, 9, 36, 84, 126, 126, 84, 36, 9, 1]

The difference between a python-iterator and a 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.