# 列表生成式
list1 =[x for x inrange(1,10)]print(list1)# 列表生产器
list2 =(x for x inrange(1,10))print(next(list2))for i in list2:print("%s----"% i)defgan():
a, b =0,1whileTrue:yield b
a, b = b,(a+b)# 生成器保存的不是数字,是生成数字的算法
g = gan()print(next(g))print(g.__next__)print(g.__next__)print(g.__next__)
from collections.abc import Iterable
from collections.abc import Iterator
a =(1,)
b =[1,2]
c ={}deftest1(arg):ifisinstance(arg, Iterable):print("%s是可迭代对象"% arg)else:print("%s不是可迭代对象"% arg)
test1(a)
test1(b)
test1(c)deftest2(arg):ifisinstance(arg, Iterator):print("%s是迭代器"% arg)else:print("%s不是迭代器"% arg)
test2(a)
test2(b)
test2(c)
test2((1for i inrange(5)))
test2(iter(a))
test2(iter(b))
test2(iter(c))
# 列表生成式list1 = [x for x in range(1, 10)]print(list1)# 列表生产器list2 = (x for x in range(1, 10))print(next(list2))for i in list2: print("%s----" % i)def gan(): a, b = 0, 1 while True: yield b a, b = b, (a+b)# 生成器保存的不是数字