How to join two generators in Python?

后端 未结 12 1571
醉酒成梦
醉酒成梦 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:26

    Here it is using a generator expression with nested fors:

    a = range(3)
    b = range(5)
    ab = (i for it in (a, b) for i in it)
    assert list(ab) == [0, 1, 2, 0, 1, 2, 3, 4]
    

提交回复
热议问题