[Python 3.4] 下面的程序是一个简单的 Eratosthenes 筛子:
from itertools import *
def excl(ns,pr):
return (i for i in ns if i%pr)
def sieve(ns):
while True:
pr=next(ns)
yield pr
ns=excl(ns,pr)
# ns=(i for i in ns if i%pr)
r=list(islice(sieve(count(2)),10))
它产生 [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]。好的。取消注释内联 excl() 的行并注释调用,给出 [2, 3, 4, 5, 6, 7, 8, 9, 10, 11]。为什么?
它是否与在循环中修改序列时预期的麻烦有关?
谢谢你的任何提示。