我将lambda函数写在外部函数里能正常筛选,但是如果直接写在filter函数里就不起作用。
运行结果:
运行结果:
代码如下:
def get_numbers():
n = 1
while True:
n += 2
yield n
def is_divisible(num):
return lambda x: x % num > 0
def primes():
yield 2
temp = get_numbers()
while True:
num = next(temp)
yield num
temp = filter(is_divisible(num), temp)
for i in primes():
if i < 100:
print(i, end = ' ')
else:
break
相关分类