我有一个算法,可以生成一个主要列表作为生成器:
def _odd_iter():
n=3
while True:
yield n
n=n+2
def _not_divisible(n):
return lambda x: x % n > 0
def primes():
yield 2
L=_odd_iter()
while True:
n=next(L)
yield n
L=filter(_not_divisible(n), L)
x=1
for t in primes():
print(t)
x=x+1
if x==10:
break
Run Code Online (Sandbox Code Playgroud)
但是如果我将lambda filter函数直接放入函数中,如下所示:
def primes():
yield 2
L=_odd_iter()
while True:
n=next(L)
yield n
L=filter(lambda x: x%n>0, L)
Run Code Online (Sandbox Code Playgroud)
我只能获得一个奇怪的列表,而不是一个主要列表.似乎filter功能不起作用.
我能做什么?