
## 埃氏筛法 求 素数 ### 从 3 开始的奇数序列 def _odd_iter(): n = 1 while True: n = n + 2 yield n ### 筛选函数 def _not_divisible(n): return lambda x: x % n > 0 ### 定义生成器 def primes(): yield 2 it = _odd_iter() # 初始序列 while True: n = next(it) # 返回序列的第一个数 yield n it = filter(_not_divisible(n), it) # 构造新序列 ### 测试结果 for n in primes(): if n < 100: print(n) else: break 目前查到关于 filter(function, sequence) 的例子并没有这种用法:
filter(_not_divisible(n), it) n 是 _odd_iter() 返回的一个 奇数,那 _not_divisible(n) 不就不是指向 函数,而是指向 函数的结果 了吗。
filter 为啥还能 如丝般运行 。
懵逼了 ;(
求一语到破人生迷思。
1 weyou 2015 年 6 月 25 日 return lambda x: x % n > 0 是返回的一个lambda函数啊,怎么会是函数结果呢?不知道你要表达什么. |
3 deanguqiang 2015 年 6 月 25 日 Python 3 的 filter 返回的是 iterator 了? |
4 deanguqiang 2015 年 6 月 25 日 @deanguqiang 查了一下,还真是。。。 |
5 Tiande OP PRO @deanguqiang p2 p3 好可怕hhhh |