gpt4 book ai didi

python - 如何将一个迭代器一分为二?

转载 作者:太空宇宙 更新时间:2023-11-04 08:55:09 24 4
gpt4 key购买 nike

如何在不迭代两次或使用额外内存存储所有数据的情况下将一个迭代器拆分为两个?

当您可以将所有内容都存储在内存中时的解决方案:

l = [{'a': i, 'b': i * 2} for i in range(10)]
def a(iterator):
for item in iterator:
print(item)
def b(iterator):
for item in iterator:
print(item)

a([li['a'] for li in l])
b([li['b'] for li in l])

或者如果你可以迭代两次,

class SomeIterable(object):
def __iter__(self):
for i in range(10):
yield {'a': i, 'b': i * 2}


def a(some_iterator):
for item in some_iterator:
print(item)


def b(some_iterator):
for item in some_iterator:
print(item)


s = SomeIterable()

a((si['a'] for si in s))
b((si['b'] for si in s))

但是如果我只想迭代一次,我该怎么做呢?

最佳答案

从注释中的说明来看,ab 是您不能重写的外部库函数,但可以交错执行。在那种情况下,你想要的是可能的,但它几乎需要线程:

import multiprocessing.pool # for ThreadPool, not multiprocessing
import Queue

_endofinput = object()

def _queueiter(queue):
while True:
item = queue.get()
if item is _endofinput:
break
yield item

def parallel_execute(funcs, iterable, maxqueue):
'''Interleaves the execution of funcs[0](iterable), funcs[1](iterable), etc.

No function is allowed to lag more than maxqueue items behind another.
(This will require adjustment if a function might return before consuming
all input.)

Makes only one pass over iterable.

'''

queues = [Queue.Queue(maxsize=maxqueue) for func in funcs]
queueiters = [_queueiter(queue) for queue in queues]
threadpool = multiprocessing.pool.ThreadPool(processes=len(funcs))

results = threadpool.map_async(lambda (f, x): f(x), zip(funcs, queueiters))

for item in iterable:
for queue in queues:
queue.put(item)

for queue in queues:
queue.put(_endofinput)

threadpool.close()
return results.get()

关于python - 如何将一个迭代器一分为二?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30856097/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com