python 交错列表合并
l1=[1,2,3] l2=['a',"b","c"] list1=list(chain.from_iterable(zip(l1,l2))) list2=[] for i in list1: list2.append(i) print(list2)
def xmerge(a, b): alen, blen = len(a), len(b) mlen = min(alen, blen) for i in range(mlen): yield a[i] yield b[i] if alen > blen: for i in range(mlen, alen): yield a[i] else: for i in range(mlen, blen): yield b[i] a = [1, 2, 3] b = [5, 6, 7, 8, 9, 10] c = [i for i in xmerge(a, b)] print (c) c = [i for i in xmerge(b, a)] print (c)
a = [1, 2, 3] b = [5, 6, 7] result = [list(zip(a, b))[i][j] for i in range(len(a)) for j in range(len(list(zip(a, b))[0]))] print(result)
三种方法足够了,如果你有其他方法也可以留言