gpt4 book ai didi

python - 有限生成器的长度

转载 作者:太空狗 更新时间:2023-10-29 21:22:29 24 4
gpt4 key购买 nike

我有这两个实现来计算有限生成器的长度,同时保留数据以供进一步处理:

def count_generator1(generator):
'''- build a list with the generator data
- get the length of the data
- return both the length and the original data (in a list)
WARNING: the memory use is unbounded, and infinite generators will block this'''
l = list(generator)
return len(l), l

def count_generator2(generator):
'''- get two generators from the original generator
- get the length of the data from one of them
- return both the length and the original data, as returned by tee
WARNING: tee can use up an unbounded amount of memory, and infinite generators will block this'''
for_length, saved = itertools.tee(generator, 2)
return sum(1 for _ in for_length), saved

两者都有缺点,但都能胜任。有人可以对它们发表评论,甚至提供更好的选择吗?

最佳答案

如果您必须这样做,第一种方法要好得多 - 当您使用所有值时,itertools.tee() 无论如何都必须存储所有值,这意味着列表将是效率更高。

引自the docs :

This itertool may require significant auxiliary storage (depending on how much temporary data needs to be stored). In general, if one iterator uses most or all of the data before another iterator starts, it is faster to use list() instead of tee().

关于python - 有限生成器的长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18014437/

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