迭代器和生成器

迭代器:

list1=[1,2,3]
print type(list1)

it = iter(list1)
print type(it)
print next(it)
print next(it)
print next(it)
print next(it)





yield 生产器:


def frange(start,stop,step):
    x=start
    while x < stop:
        yield x
        x = x + step

for x in frange(1,10,0.5):
    print x
	

def frange(start,stop,step):
    x=start
    while x < stop:
        yield x
        x = x + step

aa=iter(frange(1,10,0.5))
print next(aa)
print next(aa)
print next(aa)


C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled2/socket/fun2.py
1
1.5
2.0
原文地址:https://www.cnblogs.com/hzcya1995/p/13348288.html