Loop over 2 lists, repeating the shortest until end of longest

前端 未结 3 569
伪装坚强ぢ
伪装坚强ぢ 2020-12-05 13:37

I am sure there is an easy and obvious way to do this, but I have been googling and reading the docs and I just cannot find anything.

This is what I want to achieve:

3条回答
  •  一个人的身影
    2020-12-05 14:32

    import itertools
    la = ['a1', 'a2', 'a3', 'a4']
    lb = ['b1', 'b2']
    for i, j in zip(la, itertools.cycle(lb)):
        print('_'.join((i,j)))
    

提交回复
热议问题