【问题标题】:itertools.repeat VS itertools.cycleitertools.repeat VS itertools.cycle
【发布时间】:2016-08-11 03:14:56
【问题描述】:

itertools.repeat(n)itertools.cycle(n) 有什么区别吗?看起来,它们产生相同的输出。在我需要某个元素的 infinite 循环的情况下使用一种更有效吗?

【问题讨论】:

  • 如果您只需要一个元素的无限序列,请使用repeat,下面的答案将解释差异。
  • @JaredGoguen 我同意。 repeat 版本更清晰、更快捷。这是首选方法。

标签: python python-2.7 itertools


【解决方案1】:

简单地说,itertools.repeat 将重复给定参数,itertools.cycle 将循环遍历给定参数。不要运行这段代码,但是例如:

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, ...

【讨论】:

  • 如果你也使用“abcd”作为重复函数的参数,这个例子会更好。
【解决方案2】:

这些是等价的,但第一个更清晰,速度更快:

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

但是,cycle 可以选择重复整个序列:

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

并且repeat可以选择设置重复次数的限制:

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

此外,预期的用例也不同。

特别是,repeat 旨在为映射函数提供重复参数:

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

cycle 用于循环重复行为。这是一个返回 1/1 - 1/3 + 1/5 - 1/7 + 1/9 + ... 的 Python 3 示例:

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

后一个示例显示了所有部分如何组合在一起以创建“迭代器代数”。

希望你发现这很有启发性:-)

【讨论】:

    【解决方案3】:

    itertools.cycle() 采用 迭代器。你不能这样做,例如,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() 将一遍又一遍地重复相同的元素 - 它不是旨在遍历迭代器的元素。一遍又一遍地返回同一个对象非常好

    换句话说,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]]
    

    在执行itertools.cycle([1,2,3]) 时,会返回一个看起来像[1,2,3,1,2,3,1,2,3,...] 的无限列表(或者至少如果你能以某种方式将它放入内存中)。

    差异相当大。

    【讨论】:

      【解决方案4】:

      itertools.repeat 一遍又一遍地返回同一个对象,itertools.cycle 迭代一遍又一遍同一个对象。所以:

      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, ...
      

      因此,如果您想要多次返回一个对象的东西,请使用itertools.repeat;如果它是在某些不同对象上循环的东西,请使用itertools.cycle

      【讨论】:

        【解决方案5】:

        请参阅 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.
         |
        

        【讨论】:

          猜你喜欢
          • 2019-02-12
          • 1970-01-01
          • 2011-07-11
          • 2014-08-17
          • 2012-02-21
          • 2016-07-10
          • 1970-01-01
          • 2018-08-11
          • 1970-01-01
          相关资源
          最近更新 更多