简体   繁体   English

itertools.repeat VS itertools.cycle

[英]itertools.repeat VS itertools.cycle

Is there any difference between itertools.repeat(n) and itertools.cycle(n) ? itertools.repeat(n)itertools.cycle(n)之间有什么区别吗? As it seems, they produce the same output. 看起来,它们产生相同的输出。 Is one more efficient to use in a situation where I need an infinite loop of some element? 在我需要某个元素的无限循环的情况下,使用它会更有效吗?

Simply, itertools.repeat will repeat the given argument, and itertools.cycle will cycle over the given argument. 简单地说, itertools.repeat将重复给定的参数,而itertools.cycle将遍历给定的参数。 Don't run this code, but for example: 不要运行此代码,但例如:

from itertools import repeat, cycle

for i in repeat('abcd'): print(i)
# abcd, abcd, abcd, abcd, ...

for i in cycle('abcd'): print(i)
# a, b, c, d, a, b, c, d, ...

These are equivalent but the first is clearer and a little faster: 这些是等价的,但第一个更清晰,更快一点:

it = repeat(x)
it = cycle([x])

However, cycle has the option of repeating entire sequences: 但是, 循环可以选择重复整个序列:

it = cycle(['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'])

And repeat has the option of setting a limit on the number of repetitions: 并且repeat可以选择设置重复次数限制:

it = repeat(x, 5)         # Return five repetitions of x

Also, the intended use cases are different. 此外,预期的用例也不同。

In particular, repeat was designed to provided a repeated argument to a mapped function: 特别是, repeat旨在为映射函数提供重复参数:

it = imap(pow, repeat(2), range(10))

While cycle is intended for cyclic recurring behaviors. 循环用于循环重复行为。 Here is a Python 3 example that returns 1/1 - 1/3 + 1/5 - 1/7 + 1/9 + ... : 这是一个Python 3示例,它返回1/1 - 1/3 + 1/5 - 1/7 + 1/9 + ...

it = accumulate(map(operator.truediv, cycle([1, -1]), count(1, 2)))

The latter example shows how all the parts fit together the create an "iterator algebra". 后一个例子展示了所有部件如何组合在一起创建一个“迭代器代数”。

Hope you found this to be illuminating :-) 希望你发现这很有启发性:-)

itertools.cycle() takes an iterator . itertools.cycle()接受一个迭代器 You can't do, for example, itertools.cycle(5) - this will throw an error: 你不能这样做,例如, itertools.cycle(5) - 这会抛出一个错误:

>>> itertools.cycle(3)
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable

itertools.repeat() will repeat the same element over and over again - it is not designed to iterate through the elements of an iterator. itertools.repeat()将一遍又一遍地重复相同的元素 - 它不是为迭代遍历迭代器的元素设计的。 It is very good for returning the same object over and over again. 一遍又一遍地返回同一个对象非常好

In other words, doing itertools.repeat([1,2,3], 5) does: 换句话说,执行itertools.repeat([1,2,3], 5)可以:

>>>>[i for i in itertools.repeat([1,2,3], 5)]
[[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]]

while doing itertools.cycle([1,2,3]) returns an infinite list that looks like [1,2,3,1,2,3,1,2,3,...] (or it least it would if you could fit it in memory somehow). 在执行itertools.cycle([1,2,3])返回一个看起来像[1,2,3,1,2,3,1,2,3,...]的无限列表(或者它至少会如果你能以某种方式将它放入记忆中)。

The difference is fairly profound. 差异相当深远。

Refer to the itertools documents to know the difference. 请参阅itertools文档以了解其中的差异。

>>> import itertools
>>> help(itertools.repeat)
Help on class repeat in module itertools:

class repeat(__builtin__.object)
 |  repeat(object [,times]) -> create an iterator which returns the object
 |  for the specified number of times.  If not specified, returns the object
 |  endlessly.
 |
...
...

>>> help(itertools.cycle)
Help on class cycle in module itertools:

class cycle(__builtin__.object)
 |  cycle(iterable) --> cycle object
 |
 |  Return elements from the iterable until it is exhausted.
 |  Then repeat the sequence indefinitely.
 |

itertools.repeat returns the same object over and over again, and itertools.cycle iterates over the same object over and over again. itertools.repeat一遍又一遍地返回相同的对象,而itertools.cycle一遍又一遍地遍历同一个对象。 So: 所以:

import itertools

# Warning: infinite loop ahead
for x in itertools.repeat([1, 2, 3]):
    print(x)
    # [1, 2, 3], [1, 2, 3], ...

for x in itertools.cycle([1, 2, 3]):
    print(x)
    # 1, 2, 3, 1, 2, 3, ...

So if what you want is something that returns one object several times, use itertools.repeat ; 因此,如果您想要的是多次返回一个对象的东西,请使用itertools.repeat ; and if it's something that loops over some different object use itertools.cycle . 如果它是循环一些不同对象的东西使用itertools.cycle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM