Pythonic way to combine two lists in an alternating fashion?

前端 未结 21 3282
误落风尘
误落风尘 2020-11-22 16:13

I have two lists, the first of which is guaranteed to contain exactly one more item than the second. I would like to know the most Pythonic way to create a

21条回答
  •  没有蜡笔的小新
    2020-11-22 16:54

    My take:

    a = "hlowrd"
    b = "el ol"
    
    def func(xs, ys):
        ys = iter(ys)
        for x in xs:
            yield x
            yield ys.next()
    
    print [x for x in func(a, b)]
    

提交回复
热议问题