How do I reverse an itertools.chain object?

后端 未结 7 1064
佛祖请我去吃肉
佛祖请我去吃肉 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:19

    In theory you can't because chained objects may even contain infinite sequences such as itertools.count(...).

    You should try to reverse your generators/sequences or use reversed(iterable) for each sequence if applicable and then chain them together last-to-first. Of course this highly depends on your use case.

提交回复
热议问题