How do I reverse an itertools.chain object?

后端 未结 7 1066
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-14 16:01

My function creates a chain of generators:

def bar(num):
    import itertools
    some_sequence = (x*1.5 for x in range(num))
    some_other_sequence = (x*2.6 fo         


        
相关标签:
7条回答
  • 2021-02-14 16:32

    You cannot reverse generators by definition. The interface of a generator is the iterator, which is a container that supports only forward iteration. When you want to reverse a iterator, you have to collect all it's items first and reverse them after that.

    Use lists instead or generate the sequences backwards from the start.

    0 讨论(0)
提交回复
热议问题