import itertools def fib(): first = 0 second = 1 while 1: n=first+second yield n first=second second=n print list(itertools.islice(fib(),10))
import itertools def fib(): first, second=0, 1 while 1: yield second first, second= second, first+second print list(itertools.islice(fib(),10))
def get(n): a=0 b=1 for k in range(n): a,b=b,a+b if b>n: yield b break for i in get(100):#此处的100也是循环的次数 print i
def get(n): a=0 b=1 l.extend((a,b)) #将a、b追加至l for k in range(n): a,b=b,a+b l.append(b) if len(l)==10: yield l break l=[] for i in get(10): print i # 结果[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
版权声明:本文为博主原创文章,未经博主允许不得转载。