Pythonic way to combine two lists in an alternating fashion?

前端 未结 21 3366
误落风尘
误落风尘 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 17:02

    How about numpy? It works with strings as well:

    import numpy as np
    
    np.array([[a,b] for a,b in zip([1,2,3],[2,3,4,5,6])]).ravel()
    

    Result:

    array([1, 2, 2, 3, 3, 4])
    

提交回复
热议问题