How to join two generators in Python?

后端 未结 12 1429
醉酒成梦
醉酒成梦 2020-11-28 22:40

I want to change the following code

for directory, dirs, files in os.walk(directory_1):
    do_something()

for directory, dirs, files in os.walk(directory_2         


        
相关标签:
12条回答
  • 2020-11-28 23:36

    I think itertools.chain() should do it.

    0 讨论(0)
  • 2020-11-28 23:36

    Simple example:

    from itertools import chain
    x = iter([1,2,3])      #Create Generator Object (listiterator)
    y = iter([3,4,5])      #another one
    result = chain(x, y)   #Chained x and y
    
    0 讨论(0)
  • 2020-11-28 23:36

    All those complicated solutions...

    just do:

    for dir in directory_1, directory_2:
        for directory, dirs, files in os.walk(dir):
            do_something()
    

    If you really want to "join" both generators, then do :

    for directory, dirs, files in [
            x for osw in [os.walk(directory_1), os.walk(directory_2)] 
                   for x in osw
            ]:
        do_something()
    
    0 讨论(0)
  • 2020-11-28 23:38

    With itertools.chain.from_iterable you can do things like:

    def genny(start):
      for x in range(start, start+3):
        yield x
    
    y = [1, 2]
    ab = [o for o in itertools.chain.from_iterable(genny(x) for x in y)]
    print(ab)
    
    0 讨论(0)
  • 2020-11-28 23:38

    2020 update: Work in both python 3 and python 2

    import itertools
    
    iterA = range(10,15)
    iterB = range(15,20)
    iterC = range(20,25)
    
    ### first option
    
    for i in itertools.chain(iterA, iterB, iterC):
        print(i)
    
    # 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
    
    ### alternative option, introduced in python 2.6
    
    for i in itertools.chain.from_iterable( [iterA, iterB, iterC] ):
        print(i)
    
    # 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
    

    itertools.chain() is the basic.

    itertools.chain.from_iterables is handy if you have an iterable of iterables. For example a list of files per subdirectory like [ ["src/server.py", "src/readme.txt"], ["test/test.py"] ].

    0 讨论(0)
  • 2020-11-28 23:46

    One can also use unpack operator *:

    concat = (*gen1(), *gen2())
    

    NOTE: Works most efficiently for 'non-lazy' iterables. Can also be used with different kind of comprehensions. Preferred way for generator concat would be from the answer from @Uduse

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