Discuss / Python / 廖老师我不能理解 it = filter(_not_divisible(n), it) 里最后面的it,按我的理解上面把it赋值给了生成器,它只是第一个生成器并不是和长序列呀

廖老师我不能理解 it = filter(_not_divisible(n), it) 里最后面的it,按我的理解上面把it赋值给了生成器,它只是第一个生成器并不是和长序列呀

Topic source

def _odd_iter():#构造一个3开头的奇数数列。 n = 1 while True: n = n + 2 yield n #调用时中断,再次调用时从下面开始。 def _not_divisible(n):#构造筛选函数, return lambda x: x % n > 0 def primes(): yield 2 it = _odd_iter() # 初始序列 while True: n = next(it) # 返回序列的第一个数 yield n it = filter(_not_divisible(n), it) # 构造新序列

打印1000以内的素数:

for n in primes(): if n < 1000: print(n) else: break


  • 1

Reply