Discuss / Python / 为什么这么写会出错

为什么这么写会出错

Topic source

filter

def primes():
    yield 2
    it=iter(range(3,200,2))
    while True:
        n = next(it)
        yield n
        it = filter(lambda x: x % n > 0, it)

这样的输出为:

3
5
7
9
11
13
15
17
19
21
23
25
27
29

结果 it 这个迭代器根本就没有被改变

练习

def is_palindrome(n):
    return str(n)==str(n)[::-1]

小太郎tail

#2 Created at ... [Delete] [Delete and Lock User]

同问这样写为什么不对哇,渴望大佬解答

同问哇

同问,想半天没明白

return lambda x: x % n > 0

是返回的一个lambda函数啊,怎么会是函数结果呢


  • 1

Reply