def infiniList():
count = 0
ls = []
while True:
yield ls[:] # here I added copying
count += 1
ls.append(count) # or you could write "ls = ls + [count]" and not need to make a copy above
import itertools
print(list(itertools.islice(infiniList(), 5)))
import itertools
def infiniList():
count = 0
ls = []
while True:
yield ls
count += 1
ls = ls + [count]
print(list(itertools.islice(infiniList(), 5, 6))[0])
itertools.islice
确实做到了这一点,不过在您的示例中,您需要注意不要重复生成对不断修改的同一对象的引用:为了回答@NPE的问题,我只需要:
相关问题 更多 >
编程相关推荐