How do I reverse an itertools.chain object?

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

    Does this work in you real app?

    def bar(num):
        import itertools
        some_sequence = (x*1.5 for x in range(num))
        some_other_sequence = (x*2.6 for x in range(num))
        list_of_chains = [some_sequence, some_other_sequence]
        if num < 0:
            list_of_chains.reverse()
        chained = itertools.chain(*list_of_chains)
        return chained
    

提交回复
热议问题