def _not_divisible(n): #是否整除
return lambda x: x%n > 0
def _odd_iter(): #创建奇数序列
n = 1
while True:
n += 2
yield n
def primes(end_num): #end_num范围内的素数
if end_num < 2:
return
yield 2
it = _odd_iter()
while True:
n = next(it)
if n > end_num:
break
yield n
it = filter(_not_divisible(n),it)
PS: lambda 返回函数,:前的x 为参数
每循环获取下个素数,将列表中该素数的所有倍数筛选掉(留下不被该素数的数)