一、函数生成
# 2.定义简单函数
list = []
def fib(n):
a,b = 1,1
list.append(a)
list.append(b)
for i in range(n):
a,b=b,a+b
list.append(b)
return b
print(fib(10))
print(list)
二、迭代器生成
(1)取到100个数列
# 实现斐波拉契数列
class Fib(object):
def __init__(self, n, number1=1, number2=1):
self.n = n
self.number1 = number1
self.number2 = number2
def __next__(self):
list1 = []
for i in range(self.n):
if i ==0:
list1.append(self.number1)
elif i ==1:
list1.append(self.number2)
else:
list1.append(list1[i-1] + list1[i-2])
return list1
def __iter__(self):
return self
f = Fib(100)
print(f.__next__())
(2)取到小于100的所有数
class Fib(object):
def __init__(self):
self.a