Python的itertools.repeat的目的是什么? [英] What is the purpose of Python's itertools.repeat?

查看:58
本文介绍了Python的itertools.repeat的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于Python的 itertools.repeat() 类,我可以想到另一个同样(可能更多)可接受的解决方案来实现相同的效果.例如:

For every use I can think of for Python's itertools.repeat() class, I can think of another equally (possibly more) acceptable solution to achieve the same effect. For example:

>>> [i for i in itertools.repeat('example', 5)]
['example', 'example', 'example', 'example', 'example']
>>> ['example'] * 5
['example', 'example', 'example', 'example', 'example']

>>> list(map(str.upper, itertools.repeat('example', 5)))
['EXAMPLE', 'EXAMPLE', 'EXAMPLE', 'EXAMPLE', 'EXAMPLE']
>>> ['example'.upper()] * 5
['EXAMPLE', 'EXAMPLE', 'EXAMPLE', 'EXAMPLE', 'EXAMPLE']

在任何情况下itertools.repeat()是最合适的解决方案吗?如果是这样,在什么情况下?

Is there any case in which itertools.repeat() would be the most appropriate solution? If so, under what circumstances?

推荐答案

itertools.repeat函数是惰性的;它仅使用一项所需的内存.另一方面,(a,) * n[a] * n习惯用法在内存中创建对象的n个副本.对于五个项目,乘法习惯用法可能更好,但是如果不得不重复一百万次,您可能会注意到资源问题.

The itertools.repeat function is lazy; it only uses the memory required for one item. On the other hand, the (a,) * n and [a] * n idioms create n copies of the object in memory. For five items, the multiplication idiom is probably better, but you might notice a resource problem if you had to repeat something, say, a million times.

不过,很难想象许多 static 用于itertools.repeat.但是,itertools.repeat function 的事实使您可以在许多功能应用程序中使用它.例如,您可能具有一些库函数func,该函数对可迭代的输入进行操作.有时,您可能已经预先构造了各种项目的列表.其他时候,您可能只想对统一列表进行操作.如果列表很大,itertools.repeat将为您节省内存.

Still, it is hard to imagine many static uses for itertools.repeat. However, the fact that itertools.repeat is a function allows you to use it in many functional applications. For example, you might have some library function func which operates on an iterable of input. Sometimes, you might have pre-constructed lists of various items. Other times, you may just want to operate on a uniform list. If the list is big, itertools.repeat will save you memory.

最后,repeat使itertools文档中描述的所谓的迭代器代数"成为可能.甚至itertools模块本身也使用repeat功能.例如,以下代码作为itertools.izip_longest的等效实现给出(即使实际代码可能是用C编写的).请注意使用repeat从底部开始的七行:

Finally, repeat makes possible the so-called "iterator algebra" described in the itertools documentation. Even the itertools module itself uses the repeat function. For example, the following code is given as an equivalent implementation of itertools.izip_longest (even though the real code is probably written in C). Note the use of repeat seven lines from the bottom:

class ZipExhausted(Exception):
    pass

def izip_longest(*args, **kwds):
    # izip_longest('ABCD', 'xy', fillvalue='-') --> Ax By C- D-
    fillvalue = kwds.get('fillvalue')
    counter = [len(args) - 1]
    def sentinel():
        if not counter[0]:
            raise ZipExhausted
        counter[0] -= 1
        yield fillvalue
    fillers = repeat(fillvalue)
    iterators = [chain(it, sentinel(), fillers) for it in args]
    try:
        while iterators:
            yield tuple(map(next, iterators))
    except ZipExhausted:
        pass

这篇关于Python的itertools.repeat的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆