在这个例子中,为什么我不能用它的 return 值替换函数名?
Why can't i replace the function name by it's return value in this example?
我正在编写一个 python 程序来打印素数。这是我的代码和一些评论
# make an odd iterator
def _odd_iter():
n = 3
while True:
yield n
n = n + 2
# return a lambda funcion to find which number couldn't be divisible by p
def _not_divisible(p):
return lambda x : x % p > 0
def primes():
yield 2
it = _odd_iter() # initial sequence
while True:
n = next(it)
yield n # give the first element in the new iterator it
# When I use code below, filter seems doesn't work well.
it = filter(lambda x : x % n > 0, it)
# When I use code below, filter works well
# it = filter(_not_divisible(n), it)
# print the first 10 primes
prime = primes()
for i in range(10):
print(next(prime), end=', ')
我的主要问题与 filter
一致。当我在过滤器中使用函数 _not_divisible(n)
时,我得到了输出:
2, 3, 5, 7, 11, 13, 17, 19, 23, 29
这就是我想要的。但是,当我使用函数 _not_divisible(n)
的 return 值 lambda x: x % n > 0
而不是函数本身时,我得到了输出:
2, 3, 5, 7, 9, 11, 13, 15, 17, 19
filter
好像不行。
我也测试_not_divisible(3)
和lambda x: x % 3 > 0
是否相同:
# test if _not_divisible(n) and lambda x:x % n > 0 are same, This works fine.
ita = filter(lambda x:x % 3 > 0, range(20))
itb = filter(_not_divisible(3), range(20))
while True:
try:
print(next(ita), end=', ')
print(next(itb), end=', ')
except StopIteration:
break
它给了我一个很好的输出:
1, 1, 2, 2, 4, 4, 5, 5, 7, 7, 8, 8, 10, 10, 11, 11, 13, 13, 14, 14, 16, 16, 17, 17, 19,
19,
请问是什么原因造成的,谁能帮忙?
这是一个偷偷摸摸的。问题出在您在上次测试中硬编码的 n
。
ita = filter(lambda x:x % 3 > 0, range(20))
itb = filter(_not_divisible(3), range(20))
我们修改代码看看
ita = []
itb = []
for n in range(3, 8, 2):
ita.append(filter(lambda x:x % n > 0, range(20)))
itb.append(filter(_not_divisible(n), range(20)))
for a, b in zip(ita, itb):
print(list(a))
print(list(b))
print()
这会打印出来
[1, 2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 15, 16, 17, 18, 19]
[1, 2, 4, 5, 7, 8, 10, 11, 13, 14, 16, 17, 19]
[1, 2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 15, 16, 17, 18, 19]
[1, 2, 3, 4, 6, 7, 8, 9, 11, 12, 13, 14, 16, 17, 18, 19]
[1, 2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 15, 16, 17, 18, 19]
[1, 2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 15, 16, 17, 18, 19]
lambda 在调用时总是会查找 n
的值。当您调用 ita
中的所有 lambda 时,它们都会看到 n
在迭代结束时的值,即 7.
相比之下,当您将该值传递给函数并从内部调用 lambda 时,您最终会“冻结”该值。因为该函数有自己的本地上下文,并且 n
是调用期间传递的任何值。因此 itb
中的每个 lambda 每次都会从不同的上下文中查找 n
。
我正在编写一个 python 程序来打印素数。这是我的代码和一些评论
# make an odd iterator
def _odd_iter():
n = 3
while True:
yield n
n = n + 2
# return a lambda funcion to find which number couldn't be divisible by p
def _not_divisible(p):
return lambda x : x % p > 0
def primes():
yield 2
it = _odd_iter() # initial sequence
while True:
n = next(it)
yield n # give the first element in the new iterator it
# When I use code below, filter seems doesn't work well.
it = filter(lambda x : x % n > 0, it)
# When I use code below, filter works well
# it = filter(_not_divisible(n), it)
# print the first 10 primes
prime = primes()
for i in range(10):
print(next(prime), end=', ')
我的主要问题与 filter
一致。当我在过滤器中使用函数 _not_divisible(n)
时,我得到了输出:
2, 3, 5, 7, 11, 13, 17, 19, 23, 29
这就是我想要的。但是,当我使用函数 _not_divisible(n)
的 return 值 lambda x: x % n > 0
而不是函数本身时,我得到了输出:
2, 3, 5, 7, 9, 11, 13, 15, 17, 19
filter
好像不行。
我也测试_not_divisible(3)
和lambda x: x % 3 > 0
是否相同:
# test if _not_divisible(n) and lambda x:x % n > 0 are same, This works fine.
ita = filter(lambda x:x % 3 > 0, range(20))
itb = filter(_not_divisible(3), range(20))
while True:
try:
print(next(ita), end=', ')
print(next(itb), end=', ')
except StopIteration:
break
它给了我一个很好的输出:
1, 1, 2, 2, 4, 4, 5, 5, 7, 7, 8, 8, 10, 10, 11, 11, 13, 13, 14, 14, 16, 16, 17, 17, 19,
19,
请问是什么原因造成的,谁能帮忙?
这是一个偷偷摸摸的。问题出在您在上次测试中硬编码的 n
。
ita = filter(lambda x:x % 3 > 0, range(20))
itb = filter(_not_divisible(3), range(20))
我们修改代码看看
ita = []
itb = []
for n in range(3, 8, 2):
ita.append(filter(lambda x:x % n > 0, range(20)))
itb.append(filter(_not_divisible(n), range(20)))
for a, b in zip(ita, itb):
print(list(a))
print(list(b))
print()
这会打印出来
[1, 2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 15, 16, 17, 18, 19]
[1, 2, 4, 5, 7, 8, 10, 11, 13, 14, 16, 17, 19]
[1, 2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 15, 16, 17, 18, 19]
[1, 2, 3, 4, 6, 7, 8, 9, 11, 12, 13, 14, 16, 17, 18, 19]
[1, 2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 15, 16, 17, 18, 19]
[1, 2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 15, 16, 17, 18, 19]
lambda 在调用时总是会查找 n
的值。当您调用 ita
中的所有 lambda 时,它们都会看到 n
在迭代结束时的值,即 7.
相比之下,当您将该值传递给函数并从内部调用 lambda 时,您最终会“冻结”该值。因为该函数有自己的本地上下文,并且 n
是调用期间传递的任何值。因此 itb
中的每个 lambda 每次都会从不同的上下文中查找 n
。